我正在尝试创建一个VLOOKUP来搜索我所有工作表中的字符串。我找到了执行此操作的代码,但是我需要一个带有所有工作表名称的命名范围。但是,我正在创建的文件每周都会添加新的工作表,因此,当我添加,重命名或删除工作表时,我需要具有所有工作表名称的命名范围自动更新。
我该怎么办?
答案 0 :(得分:0)
您可以尝试:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim SearchString As String
Dim LookUpRange As Range, FoundRange As Range
Dim VLookupCol As Long
SearchString = "Bingo"
'Number of columns for VLookup
VLookupCol = 3
For Each ws In ThisWorkbook.Worksheets
'Search in each worksheet used range
With ws
Set LookUpRange = .UsedRange
'Found range
Set FoundRange = LookUpRange.Find(what:=SearchString, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
If Not FoundRange Is Nothing Then
MsgBox .Cells(FoundRange.Row, FoundRange.Column + VLookupCol).Value
End If
End With
Next ws
End Sub