【发布时间】:2020-07-02 22:37:34
【问题描述】:
我想向 VBA 宏添加一个子例程,该宏在 B2:B200 范围内的 Excel 工作表“LB Rack”中搜索值(“标记”)。 如果找到标签,我希望它运行另一个子“InsertSVblock”。 如果未找到标签,我希望它输出一条 Msgbox 消息,并运行 sub“CheckforLBmatch(tag)”,它会搜索另一张表。
我的方法是对范围执行 VLOOKUP,并检查错误 1004。
它正在工作到一定程度 - 如果找不到匹配项,它会输出错误消息,并运行“CheckforLBmatch(tag)”。但是,如果找到匹配项,我怎样才能让它运行“InsertSVblock”? 或者有更好的方法吗?
Public Sub CheckforLBmatch(tag)
Dim xlApp As excel.Application
Dim xlbook As excel.Workbook
Dim xlSht As excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlbook = GetObject("C:\07509\LB_RACKTMC.xlsx")
Set xlSht = xlbook.Sheets("LB RACK")
Set LBrng = xlSht.Range("B2:B200")
On Error GoTo ErrorHandler
Debug.Print xlApp.WorksheetFunction.VLookup(tag, LBrng, 3, False)
Exit Sub
ErrorHandler:
If Err.Number = 1004 Then
MsgBox "No SV component in LB Rack for " & tag
Err.Clear
CheckforET200match (tag)
End If
Resume Next
End Sub
【问题讨论】:
-
here 是一个很好的使用 find 的资源。它通过一个案例来检查是否找到任何东西。