Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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
VBA/Excel-查找保存文件的路径,但仅匹配路径的特定部分_Excel_Vba_Path_Fso - Fatal编程技术网

VBA/Excel-查找保存文件的路径,但仅匹配路径的特定部分

VBA/Excel-查找保存文件的路径,但仅匹配路径的特定部分,excel,vba,path,fso,Excel,Vba,Path,Fso,我想在以下示例文件夹中保存一个文件: C:\main文件夹\Subfolder1\Subfolder2\Subfolder3\u A\u abc\u 123 该文件夹中还有其他子文件夹,我希望保存该文件,如: 子文件夹_B_xyz_456 子文件夹_C_rst_789 等 问题是,我想在路径上找到一个文件夹,一直到:Subfolder3,a将从工作表中的某个范围获取,而_abc_123,我不想匹配 是否有人有一个聪明的FSO示例或其他创造性解决方案?我是编程新手,所以任何建议都非常感谢 提前谢谢

我想在以下示例文件夹中保存一个文件:

C:\main文件夹\Subfolder1\Subfolder2\Subfolder3\u A\u abc\u 123

该文件夹中还有其他子文件夹,我希望保存该文件,如:

子文件夹_B_xyz_456

子文件夹_C_rst_789

问题是,我想在路径上找到一个文件夹,一直到:Subfolder3,a将从工作表中的某个范围获取,而_abc_123,我不想匹配

是否有人有一个聪明的FSO示例或其他创造性解决方案?我是编程新手,所以任何建议都非常感谢

提前谢谢

蟒蛇样式

ho1的最新问题:

代码如下:

Sub Create_WorkB_Input()

Dim wbBook1 As Workbook
Dim wbBook2 As Workbook
Dim shTemp1 As Worksheet
Dim shTemp2 As Worksheet
Dim shTemp_admin As Worksheet
Dim shTSSR_inp1 As Worksheet
Dim shTSSR_inp2 As Worksheet
Dim strVersion As String
Dim strPrep As String
Dim Datecr As Date
Dim strComment As String
Dim intBatch As Integer
Dim strSiteID As String
Dim strClusterID As String
Dim strPath As String
Dim fso As New FileSystemObject
Dim flds As Folders
Dim f As Folder

Set wbBook1 = Workbooks("Name_Input_TEMPLATE_v4.0.xls")
Set wbBook2 = Workbooks("Name_Input_To_xxx.xlsm")
Set shTemp1 = Workbooks("Name_Input_TEMPLATE_v4.0.xls").Sheets("TSSR_Input_sh1")
Set shTemp2 = Workbooks("Name_Input_TEMPLATE_v4.0.xls").Sheets("TSSR_Input_sh2")
Set shTSSR_inp1 = Workbooks("Name_Input_To_xxx.xlsm").Sheets("xxx")
Set shTSSR_inp2 = Workbooks("Name_Input_To_xxx.xlsm").Sheets("yyy")
Set shTemp_admin = Workbooks("Name_Input_TEMPLATE_v4.0.xls").Sheets("www")

shTSSR_inp1.UsedRange.Copy

shTemp1.Paste

shTSSR_inp2.UsedRange.Copy

shTemp2.Paste

intBatch = shTemp1.Range("AQ2").Value
strSiteID = shTemp1.Range("A2").Value
strClusterID = shTemp1.Range("B2").Value
strComment = InputBox(Prompt:="Insert comments.", Title:="INSERT COMMENTS", Default:="New site - batch " & intBatch & " ref email fr Me dato")

With shTemp_admin
    .Range("A18").FormulaR1C1 = "4.0"
    .Range("B18").Value = "John Doe"
    .Range("C18").Value = Date
    .Range("D18").Value = strComment
End With

strPath = "D:\Path_to_folder\folder1\folder2\folder3\folder4"

Set flds = fso.GetFolder(strPath & "\Folder5_Input_Batch_" & intBatch & "*")

For Each f In flds


    If f.Name Like strPath Then



        wbBook1.SaveAs Filename:="" + strPath + "\" + "TSSR_Input_" + strClusterID + "_" + strSiteID + "_v4.0.xls", _
                FileFormat:=xlNormal, _
                Password:="", _
                WriteResPassword:="", _
                ReadOnlyRecommended:=False, _
                CreateBackup:=False

    End If

Next

End Sub
我在这行遇到错误:

Set flds = fso.GetFolder(strPath & "\Folder5_Input_Batch_" & intBatch & "*")
你能看一下吗? 文件夹和工作簿的名称已更改,因此它们可能没有任何意义。只有文件夹部分才重要

提前谢谢

Rgds


p

您可以在所有子目录中循环,并将每个目录与要查找的路径进行比较。类似这样的伪代码应该可以工作:

For each dir in SubDirectories
  Dim lookingFor as String
  lookingFor = "Subfolder3_" & yourVariable & "*"
  If dir.Name Like lookingFor Then ' Note the use of the Like operator here so that it sees the * as a wildcard
    ' This is the right one
  End If
Next

另一个类似的选择是使用比Like更强大的正则表达式,但我认为您不需要这样做。不过,为了以防万一,您可以在此处找到相关信息:

发布的解决方案没有问题。我只是想我也会发布另一个使用Dir函数的替代方法,这应该会快一点——特别是如果你有很多子目录要搜索的话

i、 e


我用案例选择的方法解决了这个问题。因为只有10个文件夹,我发现这是最简单的方法。like和RegEx解决方案看起来很有趣,在其他情况下会对我有很大帮助。谢谢你的帮助。
Dim strFoundDir as String

strFoundDir=dir("C:\MainFolder\Subfolder1\Subfolder2\SubFolder3*" & SomeVariable & "*", vbDirectory)
    if lenb(strFoundDir)>0 then
        'Do the rest of your code
    end if