目前,我们会收到客户用 Excel 填写的电子表格,因此我有一个装满工作簿的文件夹。
每个工作簿都有多个工作表。
我需要搜索每个工作簿以查看范围“Sheet3 上的 J8:Y8”和“Sheet 4 上的 G8:AC8”是否已填写,因为这些需要进一步审查,但只有少数人会填写这些工作表。
此外,每个工作簿都有完全不同的名字。
我在网上找到了以下代码,理论上它可以满足我的需求。但是,它会在工作簿的每一张表中搜索特定的“值”。
Sub SearchFolders()
Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
'Change as desired
strPath = "c:\MyFolder"
strSearch = "Specific text"
Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
.Cells(lRow, 4) = "Text in Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(Filename:=strPath & "\" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
For Each wks In wbk.Worksheets
Set rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
.Cells(lRow, 4) = rFound.Value
Else
Exit Do
End If
Set rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close (False)
strFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"
ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
答案1
该代码将按以下方式工作:
- 在任意位置打开一个新工作簿。
- 将 VBA 代码粘贴到宏上
在 Sheet 1 的 A1 单元格中输入工作簿文件夹的路径,例如:
C:\users\yourname\folder\
例如,在单元格 A2 上为第一个范围:
J8:Y8
在单元格 B2 上为工作表名称:Sheet3
- 例如,在单元格 A3 上为第二个范围:
G8:AC8
在单元格 B3 上为工作表名称:Sheet4
此代码的最佳之处在于,如果您有更多范围/工作表需要搜索,您可以添加下一行。
它看起来会像这样:
现在,执行宏,执行后它将显示结果工作表2,显示文件的名称和每个范围的空单元格数。
Sub foldersearch()
Dim wbk As Workbook
Dim wbk1 As Workbook
Dim wks As Worksheet
Dim wks2 As Worksheet
Dim totaltime As Long
Dim dtDuration As Date
Set wbk = ThisWorkbook
Set wks = wbk.Sheets(1)
Set wks2 = wbk.Sheets(2)
starttime = Now()
wks2.Cells.ClearContents
dirPath = wks.Cells(1, 1)
file = Dir(dirPath)
rowscounter = 0
Application.ScreenUpdating = False
While (file <> "")
If InStr(file, "xls") > 0 Then
rowscounter = rowscounter + 1
totalpath = dirPath & file
Set wbk1 = Workbooks.Open(totalpath, , True)
rangelist = True
i = 2
columnscounter = 2
While rangelist = True
thenewrango = wks.Cells(i, 1)
thenewsheet = wks.Cells(i, 2)
emptycount = workbooksearch(wbk1, thenewsheet, thenewrango)
wks2.Cells(rowscounter, 1) = file
wks2.Cells(rowscounter, columnscounter) = emptycount
i = i + 1
columnscounter = columnscounter + 1
If wks.Cells(i, 1) = "" Then
rangelist = False
End If
Wend
wbk1.Close (False)
End If
file = Dir
Wend
Application.ScreenUpdating = True
endtime = Now()
totaltime = DateDiff("s", starttime, endtime)
a = MsgBox("Finished in" & vbCrLf & totaltime & " seconds", vbOKOnly)
End Sub
Function workbooksearch(wbk1 As Workbook, wksname As Variant, rango As Variant)
Dim wks1 As Worksheet
Dim obj As Object
On Error GoTo HandleError
Set obj = wbk1.Sheets(wksname)
Set wks1 = wbk1.Worksheets(wksname)
emptycount = 0
For Each c In wks1.Range(rango)
If c.Value = "" Then
emptycount = emptycount + 1
End If
Next c
workbooksearch = emptycount
Exit Function
HandleError:
workbooksearch = "N/A"
End Function