问题
I'm searching for a string within a column of a closed Excel workbook.
The following code gives a type mismatch error on MsgBox.
If I replace that line with ret = "'" & wbPath & "[" & wbName & "]" & wsName & "'!" & Range("C3015").Address(True, True, -4150)
then the macro gives me a hard-coded value (in this case, the value at cell C3015).
How can I search for other values within columns of closed workbooks, without opening them?
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
回答1:
If your using the book in more than one macro you may want to leave the workbook open, you can do something like the following to open and hide it. You could also set the workbook to a public variable so that you can close it when your done.
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
来源:https://stackoverflow.com/questions/18069445/query-a-closed-workbook-without-opening-it