我有代码搜索excel文件的特定文件夹路径并提取结果。我无法弄清楚的是,如何选择整个目录并打开/搜索它遇到的每个文件夹。 最好的解决方案是打开文件夹的IF语句如果可用,但我很难过。
提前谢谢你。
如果我需要更具描述性,请告诉我!
Try
excelapp = New Application
excelapp.Visible = False
strPath = TextBox2.Text
'strPath = "C:\Users\asside\Documents\Test Program"
strSearch = TextBox1.Text
'strSearch = "soup"
If TextBox1.Text = "" Then
Form3.ShowDialog()
Exit Sub
End If
itms(0, 0) = "Workbook"
itms(1, 0) = "Worksheet"
itms(2, 0) = "Cell"
itms(3, 0) = "Text in Cell"
fso = CreateObject("Scripting.FileSystemObject")
fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
wbk = excelapp.Workbooks.Open(
Filename:=strPath & "\" & strFile,
UpdateLinks:=0,
ReadOnly:=True,
AddToMru:=False)
For Each wks In wbk.Worksheets
rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
Exit Do
Else
itmcnt += 1
ReDim Preserve itms(3, itmcnt)
itms(0, itmcnt) = wbk.Name
itms(1, itmcnt) = wks.Name
itms(2, itmcnt) = rFound.Address
itms(3, itmcnt) = rFound.Value
End If
rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close(False)
strFile = Dir()
Loop
Catch ex As Exception
MsgBox(ex.Message, vbExclamation, "")
End Try
wOut = Nothing
wks = Nothing
wbk = Nothing
fld = Nothing
fso = Nothing
excelapp.Visible = False
excelapp = Nothing
Dim savefilePath As String
savefilePath = Form5.TextBox1.Text
If savefilePath = "" Then
savefilePath = "Z:\Eric Application\SoupSearch\Program Files\OutputFolder\OutputSearch.CSV"
End If"
答案 0 :(得分:0)
使用搜索选项System.IO.SearchOption.AllDirectories
Dim paths = IO.Directory.GetFiles("path", "*.xls*", IO.SearchOption.AllDirectories)
在你的例子中
Try
excelapp = New Application
excelapp.Visible = False
strPath = TextBox2.Text
'strPath = "C:\Users\asside\Documents\Test Program"
strSearch = TextBox1.Text
'strSearch = "soup"
If TextBox1.Text = "" Then
Form3.ShowDialog()
Exit Sub
End If
itms(0, 0) = "Workbook"
itms(1, 0) = "Worksheet"
itms(2, 0) = "Cell"
itms(3, 0) = "Text in Cell"
fso = CreateObject("Scripting.FileSystemObject")
fld = fso.GetFolder(strPath)
For Each strfile In IO.Directory.GetFiles(strPath, "*.xls*", IO.SearchOption.AllDirectories)
wbk = excelapp.Workbooks.Open(
Filename:=strfile,
UpdateLinks:=0,
ReadOnly:=True,
AddToMru:=False)
For Each wks In wbk.Worksheets
rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
Exit Do
Else
itmcnt += 1
ReDim Preserve itms(3, itmcnt)
itms(0, itmcnt) = wbk.Name
itms(1, itmcnt) = wks.Name
itms(2, itmcnt) = rFound.Address
itms(3, itmcnt) = rFound.Value
End If
rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close(False)
Next
Catch ex As Exception
MsgBox(ex.Message, vbExclamation, "")
End Try
答案 1 :(得分:0)
使用方法IO.Directory.GetFiles
并将searchOption
- 参数设置为AllDirectories
。
For Each f As String In IO.Directory.GetFiles("C:\", "*.xls", IO.SearchOption.AllDirectories)
' Your method to pull back results
Next