Query a closed workbook without opening it

拈花ヽ惹草 提交于 2019-12-10 18:56:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!