我正在嘗試搜索文件夾(和子文件夾)中的所有excel工作簿以獲取值。VBA搜索已關閉的工作簿以獲取價值?
我的文件夾結構,在我的Excel工作簿的是,像這樣:
destPath = "G:\WH DISPO\(3) PROMOTIONS\(18) Food Specials Delivery Tracking\Archive\"
然後我的存檔文件夾內有各種各樣的子文件夾像
+ 2017
- April
- May
+ 2016
- April
- May
工作簿的名稱可能都不同,所以代碼將需要可能使用通配符* .xlsm
這是我到目前爲止:
Sub Search()
Dim srcWorkbook As Workbook
Dim destWorkbook As Workbook
Dim srcWorksheet As Worksheet
Dim destWorksheet As Worksheet
Dim SearchRange As Range
Dim destPath As String
Dim destname As String
Dim destsheet As String
Set srcWorkbook = ActiveWorkbook
Set srcWorksheet = ActiveSheet
Dim vnt_Input As String
vnt_Input = Application.InputBox("Please Enter Client Name", "Client Name")
destPath = "G:\WH DISPO\(3) PROMOTIONS\(18) Food Specials Delivery Tracking\Archive\"
destname = "*.xlsm"
On Error Resume Next
Set destWorkbook = ThisWorkbook
If Err.Number <> 0 Then
Err.Clear
Set wbTarget = Workbooks.Open(destPath & destname)
CloseIt = True
End If
For Each c In wbTarget.Sheets(1).Range("A:Q") 'No need for the .Cells here
If InStr(c, vnt_Input) > 0 Then 'vnt_Input is a variable that holds a string, so you can't put quotes around it, or it will search the string for "vnt_Input"
MsgBox "Found"
End If
Next c
End Sub
每個工作簿中的範圍應始終保持不變。
我在嘗試一些簡單的事情,比如在找到值時顯示一條消息。但目前,儘管工作簿中存在價值,但我得不到任何結果/消息。
我在這條線得到一個對象所需的錯誤:
For Each c In wbTarget.Sheets(1).Range("A:Q") 'No need for the .Cells here
請能有人告訴我在哪裏,我錯了?
編輯:
我可以改變消息框的每個循環列出每個結果像這樣:
Dim i As Integer
For i = 20 To 100
For Each rngFound In rngFound
ThisWorkbook.ActiveSheet.Range("E" & i).Value = "1 Result found for " & rngFound & " in " & wbTarget.Path & "\" & wbTarget.Name & ", on row " & rngFound.Address
Next rngFound
Next i
所需的結果
寫'頂部選項Explicit',然後嘗試調試。你必須定義'CloseIt'和'c',可能還有別的。 HTTP://計算器。com/questions/1139321/how-do-i-force-vba-access-to-require-variables-to-be-defined – Vityata
收集字符串中的所有位置並在末尾打印它們可能會更好,或者你想每次都停下來,一旦找到價值就做點什麼?如果這是您需要的功能,那麼很難停止中間代碼並更新工作表。 – User632716
@tompreston在功能方面,我只是想顯示一個消息給出的工作簿的名稱和文件路徑的值 – user7415328