【问题标题】:Set variable column in search looping through worksheets not working,在搜索循环中设置变量列不工作,
【发布时间】:2019-02-05 11:21:52
【问题描述】:

我正在研究一个宏,该宏需要在“sheet1”的范围内查找每个值,并在工作簿的所有工作表中搜索它(可能最多 7 个工作表),因为它花费的时间太长,看起来对于整个工作表中的值,我想将每张工作表上的搜索字段减少到一个带有标题 IP 的变量列。

这是我目前拥有的宏,但我不能让它只在指定范围内工作,但是,删除这些行会使宏工作正常。

提前谢谢你。

Sub findInventory()
Dim ws As Worksheet
Dim strWhat, rngFound, mString As String
Dim rngSearch, osfind, rfind, rfcol As Range
Dim i, x As Integer
Dim LastRow, oscol, lcol, e, lrowA, remChar, fcol As Long




Sheets("GVM Report").Cells(1, 1).Offset(0, 1).Resize(, 2).EntireColumn.Insert
Sheets("GVM Report").Cells(1, 1).Offset(0, 1).Value = "INVENTORY"
Sheets("GVM Report").Cells(1, 1).Offset(0, 2).Value = "OPSDB"

Set rfind = ActiveWorkbook.Sheets("GVM Report").Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
lcol = rfind.Column

Set osfind = ActiveWorkbook.Sheets("GVM Report").Rows("1:3").Find(What:="OS*", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
oscol = osfind.Column

LastRow = Sheets("GVM Report").Range("A" & Rows.Count).End(xlUp).Row

For x = 2 To LastRow

strWhat = Sheets("GVM Report").Cells(x, lcol)
    For Each ws In Worksheets
    Set rfcol = ws.Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    fcol = rfcol.Column
    With ws.Columns(fcol)
    Select Case ws.name
    Case "Operations", "Data", "FYI all OS", "Unique Values", "GVM Report"

    Case Else
       Set rngSearch = ws.Cells.Find(What:=strWhat)
        If strWhat <> "" Then
            If Not rngSearch Is Nothing Then
                i = i + 1
                If i = 1 Then
                    rngFound = rngSearch.Worksheet.name
                Else
                    rngFound = rngFound & " | " & rngSearch.Worksheet.name
                End If
                    End If

        End If

                Sheets("GVM Report").Cells(x, 2) = rngFound


    End Select
    End With
    Next ws
    rngFound = ""
    i = 0
Next x
End Sub

【问题讨论】:

    标签: excel vba loops find


    【解决方案1】:

    我用虚拟数据做了一个测试,得到了以下结果:

    INVENTORY
    Sheet2 | Sheet3
    Sheet2
    Sheet3
    

    考虑到我创建的虚拟数据,这与我所期望的一致。也许有前导、尾随空格或其他不可见的差异会导致看似相等的单元格上的数据不匹配?

    【讨论】:

      【解决方案2】:

      我不确定是否可以复制您的错误,但我已经稍微整理了您的代码,它可能对您有用,所以这里是:

      Sub findInventory()
      Dim ws As Worksheet
      Dim wsGVM As Worksheet: Set wsGVM = ThisWorkbook.Sheets("GVM Report")
      Dim strWhat As String, rngFound As String, mString As String
      Dim rngSearch As Range, osfind As Range, rfind As Range, rfcol As Range
      Dim i As Integer, x As Long, LastRow As Long, oscol As Long, lcol As Long, fcol As Long
      
      wsGVM.Cells(1, 1).Offset(0, 1).Resize(, 2).EntireColumn.Insert
      wsGVM.Cells(1, 1).Offset(0, 1).Value = "INVENTORY"
      wsGVM.Cells(1, 1).Offset(0, 2).Value = "OPSDB"
      
      Set rfind = wsGVM.Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
      If Not rfind Is Nothing Then lcol = rfind.Column
      
      Set osfind = wsGVM.Rows("1:3").Find(What:="OS*", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
      If Not osfind Is Nothing Then oscol = osfind.Column
      
      LastRow = wsGVM.Range("A" & Rows.Count).End(xlUp).Row
      
      For x = 2 To LastRow
          strWhat = wsGVM.Cells(x, lcol)
          For Each ws In Worksheets
              Set rfcol = ws.Rows("1:3").Find(What:="IP", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
              If Not rfcol Is Nothing Then
                  fcol = rfcol.Column
                  With ws.Columns(fcol)
                      Select Case ws.Name
                          Case "Operations", "Data", "FYI all OS", "Unique Values", "GVM Report"
                          ''''
                          Case Else
                              If strWhat <> "" Then
                                  Set rngSearch = .Find(What:=strWhat)
                                  If Not rngSearch Is Nothing Then
                                      i = i + 1
                                      If i = 1 Then
                                          rngFound = rngSearch.Worksheet.Name
                                      Else
                                          rngFound = rngFound & " | " & rngSearch.Worksheet.Name
                                      End If
                                  End If
                              End If
                              wsGVM.Cells(x, 2) = rngFound
                      End Select
                  End With
              End If
          Next ws
          rngFound = ""
          i = 0
      Next x
      End Sub
      

      【讨论】:

      • 您好,谢谢!但在这两种情况下,它都没有提供任何信息,虽然在设置列之前,它在“INVENTORY”列中带来了正确的结果,就像没有找到正确的结果,列范围包含 ip 地址,并且在匹配 ip 时,它应该带上找到值的工作表名称。
      • 谢谢你,这真是奇迹!我一直在尝试使用已经修剪过的报告!我再次尝试了完整的报告并带来了正确的结果!
      猜你喜欢
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多