我需要写一个宏来计算文件名中包含“财务”的文件。
我尝试遵循以下代码,但是它不起作用。我不知道这是错还是正确。
范围(“ B6”)是财务。
Dim FolderPath As String
Dim Path As String
Dim count As Integer
Dim countFile As Integer
Dim Folder As String
FolderPath = Range("B2")
Path = FolderPath & "\*.xls"
Filename = Dir(Path)
If Folder = vbNullString Then
Range("C6").Interior.ColorIndex = 4
Else
Range("C6").Interior.ColorIndex = 3
End If
Found0 = Dir(FolderPath & "\" & "*" & Range("B6") & "*")
Do While Filename = Found0
count = count + 1
Filename = Dir()
Loop
If count = 6 Then
Range("C6").Interior.ColorIndex = 4
Else
Range("C6").Interior.ColorIndex = 3
End If
Range("C6").Value = count
答案 0 :(得分:0)
尝试一下
Sub Test_CountFiles_UDF()
CountFiles ThisWorkbook.Path & "\MyFolder\", "xls*,doc*"
Debug.Print "--------"
CountFiles ThisWorkbook.Path & "\MyFolder\", "xls*,doc*", "finance"
End Sub
Sub CountFiles(sPath As String, sExtensions As String, Optional sFindText As String)
Dim arrTypes, strFile As String, i As Long, c As Long
arrTypes = Split(sExtensions, ",")
For i = 0 To UBound(arrTypes)
c = 0
strFile = Dir(sPath & "*." & arrTypes(i))
Do While strFile <> ""
If Not IsMissing(sFindText) And sFindText <> "" Then
If InStr(LCase(strFile), LCase(sFindText)) Then c = c + 1
Else
c = c + 1
End If
strFile = Dir
Loop
Debug.Print arrTypes(i) & ": " & c
Next i
End Sub