我正在处理一个宏,该宏需要查找“ 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
答案 0 :(得分:0)
我不确定是否可以复制您的错误,但是我已经整理了一下您的代码,它可能对您有用,所以在这里:
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
答案 1 :(得分:0)
我用虚拟数据进行了测试,并得到以下结果:
INVENTORY
Sheet2 | Sheet3
Sheet2
Sheet3
与我创建的虚拟数据所期望的一致。也许存在前导,尾随空格或其他不可见的差异,导致看起来相等的单元格上的数据不匹配?