【问题标题】:Looping over column headers one sheet and finding exact matches on another sheet VBA code在一张纸上循环列标题并在另一张纸上找到完全匹配的 VBA 代码
【发布时间】:2018-10-29 13:54:55
【问题描述】:

我看过几篇关于在 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    试试,

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

    VBA .Find '记住'用户使用的最后一个工作表查找 (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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多