Excel vba搜索文件名的一部分以查看文件是否存在?

Excel vba搜索文件名的一部分以查看文件是否存在?,excel,vba,text,Excel,Vba,Text,我有几个文本文件(.txt),都称为“Log”,后面跟一个日期(2014年11月)和一个随机数(34450) 因此,我的文件夹目录(p:)看起来像: Log (11-2014) 12234.txt Log (10-2014) 45546.txt Log (08-2014) 686868.txt Log (11-2014) 343434.txt 我要做的是使用vba代码统计日志文件包含今天日期的相同月份和年份的所有事件 所以今天的月份是11,今天的年份是2014 因此,我想计算文件名(11-20

我有几个文本文件(.txt),都称为“Log”,后面跟一个日期(2014年11月)和一个随机数(34450)

因此,我的文件夹目录(p:)看起来像:

Log (11-2014) 12234.txt
Log (10-2014) 45546.txt
Log (08-2014) 686868.txt
Log (11-2014) 343434.txt
我要做的是使用vba代码统计日志文件包含今天日期的相同月份和年份的所有事件

所以今天的月份是11,今天的年份是2014

因此,我想计算文件名(11-2014)的日期位与当前日期/今天日期的月份和年份匹配的所有日志文件

这是我尝试过的,但它不起作用,即使文件不存在,我也会不断被“找到”,请有人告诉我我做错了什么

 Dim iMonth As Integer
    Dim iYear As Integer

    Dim target As String
    iMonth = Month(Date)
    iYear = Year(Date)

    thefile = "P:\Log *(" & iMonth & "-" & iYear & ")"
    If thefile > 0 Then

    MsgBox "Found"

    Else

    MsgBox "Not"

    End If

可以使用Left或InStr函数查找子字符串是否是另一个字符串的一部分

dim logName as string, logNameToFind as string 
if Left(logName, Len(logNameTofind)) = LogNameToFind then 
      MsgBox "Found"
end if  

其中logName是在磁盘上找到的文件名,logNameToFind是您要查找的模式


要从目录中获取所有文件,请使用函数

您可以使用下面的函数用与您的模式匹配的所有文件填充数组,然后使用
UBound(myArray)
获取计数

Private Function GetFileList(FileSpec As String) As Variant
'   Returns an array of filenames that match FileSpec
'   If no matching files are found, it returns False

    Dim FileArray() As Variant
    Dim FileCount As Integer
    Dim FileName As String

    On Error GoTo NoFilesFound

    FileCount = 0
    FileName = Dir(FileSpec)
    If FileName = "" Then GoTo NoFilesFound

    'Loop until no more matching files are found
    Do While FileName <> ""
        FileCount = FileCount + 1
        ReDim Preserve FileArray(1 To FileCount)
        FileArray(FileCount) = FileName
        FileName = Dir()
    Loop
    GetFileList = FileArray
    Exit Function

NoFilesFound:
    GetFileList = False
End Function
私有函数GetFileList(FileSpec作为字符串)作为变量
'返回与FileSpec匹配的文件名数组
'如果找不到匹配的文件,则返回False
Dim FileArray()作为变量
Dim FileCount为整数
将文件名设置为字符串
在出现错误时转到找不到文件
FileCount=0
FileName=Dir(FileSpec)
如果FileName=“”,则转到NoFilesFound
'循环,直到找不到更多匹配的文件
文件名“”时执行此操作
FileCount=FileCount+1
ReDim保留文件数组(1到文件计数)
FileArray(FileCount)=文件名
FileName=Dir()
环
GetFileList=FileArray
退出功能
找不到文件:
GetFileList=False
端函数

使用以下URL检查文件是否存在,您可以在此添加计数器以获取相同类型名称的文件计数。网址:
Private Function GetFileList(FileSpec As String) As Variant
'   Returns an array of filenames that match FileSpec
'   If no matching files are found, it returns False

    Dim FileArray() As Variant
    Dim FileCount As Integer
    Dim FileName As String

    On Error GoTo NoFilesFound

    FileCount = 0
    FileName = Dir(FileSpec)
    If FileName = "" Then GoTo NoFilesFound

    'Loop until no more matching files are found
    Do While FileName <> ""
        FileCount = FileCount + 1
        ReDim Preserve FileArray(1 To FileCount)
        FileArray(FileCount) = FileName
        FileName = Dir()
    Loop
    GetFileList = FileArray
    Exit Function

NoFilesFound:
    GetFileList = False
End Function