查找包含特定值的单元格范围

时间:2018-01-25 05:21:07

标签: excel vba excel-vba

我是VBA的新手,所以我不熟悉它的所有功能。我有一个包含许多“表格”的工作表。通过表格,我不是指实际的Excel表格对象,而是通过颜色/边框格式分隔为“表格”的数据块。

通过查找包含“RefNum:”的单元格,我可以找到特定表开始的单元格。但是,为了避免错误检测表,我想仔细检查它之后的下一个单元格。

基本上,我想要的不只是找到“RefNum:”而是找到包含正确顺序的ff的3x1数组的位置:

 - RefNum:
 - Database:
 - ToolID:

只有这样才能确定我找到的是真正的桌子。

我正在考虑找到“RefNum:”并做if-else进行验证,但也许有更复杂的方法可以做到这一点?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

试试这段代码:

Sub FindTables()
Dim cell As Range
Dim firstAddress As String

With Range(Cells(1, 1), Cells(Rows.Count, Columns.Count))
Set cell = .Find("RefNum", LookIn:=xlValues)
firstAddress = cell.Address
Do
    'check cell next to "RefNum" and one after that
    If LCase(cell.Offset(0, 1).Value) = "database" And LCase(cell.Offset(0, 2).Value) = "toolid" Then
        'here, cell is first cell (ref num) of the table
        cell.Interior.ColorIndex = 4
    End If
    Set cell = .FindNext(cell)
Loop While Not cell Is Nothing And cell.Address <> firstAddress

End With

End Sub

答案 1 :(得分:0)

根据Michal的代码,这是我想出的答案。除了一件事,它运作良好。它不检测第一个单元格地址,只检测第二个单元格地址。任何人都可以看到我犯了错误吗?

Option Explicit

Public Sub LogSum()

'Declare variables
Dim shtMacro As Worksheet       'Sheet where macro button is located
Dim Fname As Variant            'List of user-selected files
Dim bookLOG As Workbook         'Active logsheet file
Dim shtLOG As Worksheet         'Active worksheet from current active workbook
Dim WS_Count As Integer         'Number of worksheets in active workbook
Dim CellDB As Range             'First cell output for find "RefNum"
Dim FirstAddress As String      'Address of the first CellDB
Dim i As Integer, j As Integer  'Loop iterators


'Prompt user to get logsheet filenames
Fname = Application.GetOpenFilename("ALL files (*.*), *.*,Excel Workbook (*.xlsx), *.xlsxm,Excel 97-2003 (*.xls), *.xls", , "Open Logsheet Files", , True)
If (VarType(Fname) = vbBoolean) Then Exit Sub
DoEvents

'Iterate per workbook
For i = LBound(Fname) To UBound(Fname)
    Set bookLOG = Workbooks.Open(Filename:=Fname(i), UpdateLinks:=0, _
                  ReadOnly:=True, IgnoreReadOnlyRecommended:=True)      'Open workbook i
    WS_Count = bookLOG.Worksheets.Count                                 'Store max number of sheets
    Debug.Print bookLOG.Name                                            'Print the workbook filename in log

    'Iterate per worksheet in workbook i
    For j = 1 To WS_Count
        Debug.Print bookLOG.Worksheets(j).Name  'Print the current sheet in log
        Set CellDB = bookLOG.Worksheets(j).UsedRange.Find("RefNum:", LookIn:=xlValues) 'Search for "RefNum:"
        If (Not (CellDB Is Nothing)) Then
            bookLOG.Worksheets(j).UsedRange.Select
            Debug.Print "Something's found here."
            FirstAddress = CellDB.Address 'Assign the 1st search address
            Debug.Print FirstAddress
            Do   'Check cell next to "RefNum:" and one after that
                If CellDB.Offset(1, 0).Value = "DATABASE: " And CellDB.Offset(2, 0).Value = "Tester:" Then
                    Debug.Print "Yay! Got You"
                    Debug.Print CellDB.Address
                Else
                    Debug.Print "Oops. False Alarm"
                End If
                Set CellDB = bookLOG.Worksheets(j).UsedRange.FindNext(CellDB)
            Loop While CellDB.Address <> FirstAddress
        Else
            Debug.Print "Nothing found here."
        End If
    Next j
Next i

End Sub
相关问题