我正在一个封闭的Excel工作簿的列中搜索一个字符串。
以下代码在MsgBox上给出了类型不匹配错误。
如果我用ret = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" & Range("C3015").Address(True, True, -4150)
替换该行,那么宏给我一个硬编码值(在这种情况下,单元格C3015的值)。
如何在不打开工作簿的情况下搜索已关闭工作簿列中的其他值?
Dim wbName As String, wbPath As String, wsName As String
wbPath = "Path\To\Workbook\"
wbName = "NameOfWorkbook.xlsb"
wsName = "NameOfWorkSheet"
Dim ret As String
ret = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" & Range("D:D").Find(What:="SearchColumnDForThisString")
MsgBox ExecuteExcel4Macro(ret) // <--------- TYPE MISMATCH ERROR
如果您在多个宏中使用该书,您可能希望保持工作簿处于打开状态,您可以执行以下操作来打开和隐藏它。您还可以将工作簿设置为公共变量,以便在完成后关闭它。
Dim Wn as Window
Dim Wb as Workbook
Application.ScreenUpdating = False
Set Wb = Application.Workbooks.Open("your book")
For Each Wn in Wb
Wn.Visible = False
Next Wn
Application.ScreenUpdating = True