使用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:"\localAdressfolder1folder2folder3" & 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"保存到静态网络目录(\localAdressfolder1folder2folder3BLOC-A)现在我只想打电话给"BLOC xxx";根据第一对应单元格中的用户输出。

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

当然,语法错误在于您必须更改:

ActiveWorkbook.SaveAs Filename:

至:

ActiveWorkbook.SaveAs Filename:=

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

MyFile = MyFile1 + MyFile2 + MyFile3 + MyFile4

至:

MyFile = MyFile3 & MyFile4

因此,最后的声明应该是:

ActiveWorkbook.SaveAs Filename:="\localAdressfolder1folder2folder3" & MyFileDir & "" & MyFile

最新更新