【发布时间】:2020-11-16 12:57:55
【问题描述】:
我想使用 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
***注:
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”。
但问题是文件名路径 ligne 中产生的语法错误,当我添加“& MyFileDir”时。 有什么建议可以实现吗?
【问题讨论】:
-
不清楚
MyFile1...MyFile4的内容是什么。请阅读minimal reproducible example 并给出一个完整的示例,包括示例输入数据和所需的输出。 -
不太清楚你想要完成什么。是否要保存名称为
MyFile1, Myfile2 etc.的活动工作簿?所有这些都在同一路径中(“\\localAdress\folder1\folder2\folder3\”)?MyFile想成为什么?无论如何,独立于上述澄清请求,您必须设置要保存的工作簿扩展... -
MyFile1…MyFile4 的内容为单元格值(Cell D2,E2,F2,G2...)
-
这是您问题中唯一明确的问题。这些字符串要如何使用?像构建路径中的子文件夹名称一样?喜欢在同一路径中保存工作簿的不同名称吗?或者,如果上述任何假设都不是您想要的?
-
我还是什么都不懂...如果,让我们说
MyFile1 = "Test1"和MyFile2 = "Test2",你想像Test1\Test2那样在路径中使用它们吗?还是像“Test1Test2”这样的简单串联名称?独立于解释这部分,这些文件夹是否已经创建?您想要在SaveAs方法期间创建它们的代码吗?