Find value in background workbook

帅比萌擦擦* 提交于 2019-12-12 01:54:17

问题


I am struggling to create a code to search for a value within a workbook that I am accessing without bringing it to the foreground. I am using the following code to access the file as "xl0":

'DATABASE ACCESS
Dim xl0 As New Excel.Application
Dim xlw As New Excel.Workbook
Dim db_directory As String
db_directory = "R:\New Quality Management System\xls\Supplier Non-Conformance\Supplier Non-Conformance Database.xlsm"
Set xlw = xl0.Workbooks.Open(db_directory)

I have successfully had this working to add records to the xl0 spreadsheet however I also need to create a modify record macro to "find" an index value and extract the row number. I tried several variations (2 examples below) with no success, can anyone advise?

Eg 1.

Windows("Supplier Non-Conformance Database.xlsm").Activate
Cells.Find(What:="2015-16-46", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

Eg 2.

xl0.Worksheets("SNCR Log").Range("B:B").Find(What:="2015-16-46",
After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

Thanks in advance,

Dan


回答1:


Here is a start : (btw, the activation is really greedy in resources, so try to avoid it as much as possible!)

Application.ScreenUpdating=False

'DATABASE ACCESS
Dim xl0 As New Excel.Application
Dim xlw As New Excel.Workbook
Dim db_directory As String

xl0.Visible=False

db_directory = "R:\New Quality Management System\xls\Supplier Non-Conformance\Supplier Non-Conformance Database.xlsm"
Set xlw = xl0.Workbooks.Open(db_directory)

MsgBox xlw.Sheets(1).Cells.Find(What:="2015-16-46", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row


Application.ScreenUpdating=True


来源:https://stackoverflow.com/questions/29488845/find-value-in-background-workbook

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