使用 vba 中的单元格值进行动态文件保存

Dynamic file saving using cells values in vba

我想使用 vba 中的单元格值实现动态文件保存到对应的文件夹名称,如下所示:

Sub Envoie_Formulaire()
        Dim MyFile1 As String
        Dim MyFile2 As String
        Dim MyFile3 As String
        Dim MyFile4 As String
        Dim MyFileDir As String
        Dim MyFile As String
        MyFile1 = Range("D2").Text
        MyFile2 = Range("E2").Text
        MyFile3 = Range("F2").Text
        MyFile4 = Range("G2").Text
        MyFileDir = MyFile1 + MyFile2
        MyFile = MyFile1 + MyFile2 + MyFile3 + MyFile4
       ' Do not display the message about overwriting the existing file.
     
       ' Save the active workbook with the name of the
       ' active workbook. Save it on the E drive to a folder called
       ' "User" with a subfolder called "JoeDoe."
       ActiveWorkbook.SaveAs Filename:"\localAdress\folder1\folder2\folder3\" & MyFileDir "\" & MyFile" "\"
       ' Close the workbook by using the following.
       Application.DisplayAlerts = False
End Sub

*** N.B:

D2 = the word  "BLOC-"
E2 = A letter from "A" to "N"
F2 =the word "BUREAU"
G2=a number of office from "1 to 1000".

所以我将动态 filename "BLOC-XXX BUREAU-XXX" 保存到静态网络目录 (\localAdress\folder1\folder2\folder3\BLOC-A) 现在我只想从第一个相应单元格中的用户输出调用“BLOC-xxx”。

但问题是当我添加 " & MyFileDir ""` 时文件名路径 ligne 中产生的语法错误。 有什么建议可以实现吗?

肯定是语法错误,您必须更改:

ActiveWorkbook.SaveAs Filename:

至:

ActiveWorkbook.SaveAs Filename:=

根据你的变量名,我认为你应该更改:

MyFile = MyFile1 + MyFile2 + MyFile3 + MyFile4

至:

MyFile = MyFile3 & MyFile4

因此最后的陈述应该是:

ActiveWorkbook.SaveAs Filename:="\localAdress\folder1\folder2\folder3\" & MyFileDir & "\" & MyFile