【发布时间】:2019-06-27 22:47:55
【问题描述】:
我需要在目录中的文件中搜索字符串的出现并返回计数。
为了进行测试,我将 4 个工作簿和 5 个工作表放入 C:\test 目录。我正在寻找工作簿中任何地方出现氨这个词的计数。我正在使用的代码正在重新调整“0”,即使我确定它存在。我相信它是因为lookin 不适用于合并的单元格。有什么技巧可以让这项工作发挥作用吗?
Sub LoopThroughFiles()
Range("'sheet1'!A6:M10000").ClearContents
Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
directory = "C:\Test\"
fileName = Dir(directory & "*.xl??")
i = 5
Do While fileName <> ""
i = i + 1
If fileName <> "" Then
Dim wbk As Workbook
With wbk
Set wbk = Workbooks.Open(directory & fileName)
End With
Dim sh As Worksheet
Dim found As Range
Dim count As Integer
For Each sh In wbk.Worksheets
Set found = sh.Cells.Find(what:="Ammonia", LookIn:=xlFormulas)
If Not found Is Nothing Then
sh.Activate
found.Select
count = count + sh.Range(found.Address).Offset(0, 3).Value
Else
End If
Next sh
wbk.Close
End If
fileName = Dir()
Loop
Range("'Sheet1'!C2").Value = count
End Sub
代码未在合并单元格中找到值。
【问题讨论】:
-
这可能与
LookIn:=xlFormulas有关。你试过xlValues吗?