Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用InStr方法检查文件名中是否存在字符串_String_Vba - Fatal编程技术网

String 使用InStr方法检查文件名中是否存在字符串

String 使用InStr方法检查文件名中是否存在字符串,string,vba,String,Vba,从特定文件夹检索文件。我想遍历文件夹,如果文件包含特定单词,则将该信息放在相应的区域中。我正在使用InStr函数和Dir。StrFile=Dir抛出了一个错误,我想这是因为我在代码前面使用了“Dir”函数,但我不知道如何使它工作。下面是我的代码。谢谢你的帮助 Sub retrieveFiles() StrFile = Dir("\\ldnfi82saua\groupRates_e\Simp\CCARArchive\ICA\*crmr_s*") Do While Len(StrFile) >

从特定文件夹检索文件。我想遍历文件夹,如果文件包含特定单词,则将该信息放在相应的区域中。我正在使用InStr函数和Dir。StrFile=Dir抛出了一个错误,我想这是因为我在代码前面使用了“Dir”函数,但我不知道如何使它工作。下面是我的代码。谢谢你的帮助

Sub retrieveFiles()
StrFile = Dir("\\ldnfi82saua\groupRates_e\Simp\CCARArchive\ICA\*crmr_s*")
Do While Len(StrFile) > 0
    If InStr("Internal1", Dir(StrFile)) = 1 Then
        Debug.Print StrFile
        Count = Count + 1
    End If
StrFile = Dir
Loop
MsgBox Count
End Sub

你的努力很接近。此代码将执行您需要的操作:

Sub retrieveFiles()
   Dim strFile As String
   Dim Count As Integer

   strFile = Dir("\\ldnfi82saua\groupRates_e\Simp\CCARArchive\ICA\*crmr_s*")

   Do While Len(strFile) > 0
      If InStr(1, strFile, "Internal1") > 0 Then
         Debug.Print strFile
         Count = Count + 1
      End If

      strFile = Dir
   Loop

   MsgBox Count
End Sub

如果Instr(StrFile,“Internal1”)>0,则。。。第一个字符串参数是正在搜索的字符串,第二个是正在搜索的字符串。如果
Instr…>0
,找到了搜索的字符串。按我15分钟前说的原话:)