【发布时间】: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
【问题讨论】: