将列标题循环到一个工作表并在另一个工作表VBA代码上查找完全匹配

时间:2018-05-19 07:31:12

标签: excel vba

我见过几篇关于在VBA中找到完全匹配的帖子但找不到我想要的内容。我有两张excel表。工作表1有两列(A和B),并且多个单词由括号分隔的多行。工作表2在A到Z列中有几行。我想从工作表2中获取每个列标题并查看它是否出现在工作表1中的任何位置,如果是,那么我对工作表2上的该列不做任何操作。如果没有出现在表1中,我想从表2中删除整个列。到目前为止我的代码工作正常,但它不区分大小写。我需要它区分大小写。

Sub findWords()
    Dim i As Long
    Dim v As Variant, r As Range, rWhere As Range
    For i = 26 To 1 Step -1
        v = Sheets("Sheet2").Cells(1, i).Value

        Set rWhere = Sheets("Sheet1").Range("A:B")

        Set r = rWhere.Find(what:=v, After:=rWhere(1))

        If r Is Nothing Then
            Cells(1, i).EntireColumn.Delete
        Else
        'do nothing'
    End If
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

尝试,

Set r = rWhere.Find(what:=v, After:=rWhere(1), matchcase:=true, lookat:=xlwhole)

VBA。记得'记得'最后一个工作表查找(ctrl + F)用户使用的设置。最好具体说明尽可能多的参数(即选项)。更多信息Range.Find Method

Sub findWords()
    Dim i As Long
    Dim v As Variant, r As Range, rWhere As Range

    Set rWhere = Sheets("Sheet1").Range("A:B")

    For i = 26 To 1 Step -1
        v = Sheets("Sheet2").Cells(1, i).Value
        Set r = rWhere.Find(what:=v, After:=rWhere(1), matchcase:=true, lookat:=xlwhole)

        If r Is Nothing Then
            Sheets("Sheet2").Cells(1, i).EntireColumn.Delete
        Else
            'do nothing'
        End If
    Next i

End Sub