这是我在这个网站上的第二篇文章,也是我对VBA的新手。
我今天的问题是,
如何将单元格值添加到路径字符串以指定要保存工作簿的文件夹。
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Path = "D:folder1folder2ProjectsThe FILEStheFILES"FileName1"
FileName1 = Range("B6")
FileName2 = Range("A1")
ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
提前感谢!
对所述问题的答案
Path = "D:folder1folder2ProjectsThe FILEStheFILES" & FileName1 & ""
另请参阅其他反馈和更正的代码以获取其他反馈。
长解释回答
当你为字符串变量赋值时,最常见的方法是这样的:
string = "This is my string value."
但是,您可能经常在代码中看到具有相当长的字符串(语法,例如在以下代码中),以允许所有文本适合开发屏幕而无需滚动:
string = "This is my really, really, really long string value. I am making this " _
& "as long as I can, while also having something to write."
如果您删除了_
,并将所有内容移动到一行,则如下所示:
string = "This is my really, really, really long string value. I am making this " & "as long as I can, while also having something to write."
请注意,分配给变量的任何字符串都可以按以下方式分解:
string = "This is" & " my " & "string value."
' Returns the same result as:
string = "This is my string value."
此外,如果我有一个字符串变量str_val = " my "
,那么我可以使用替换将上面的示例编写为:
string = "This is" & str_val & "string value."
其他反馈
目前,代码的顺序(请参阅下面的原始代码)为:
- 定义变量
- 为路径赋值
- 为文件名分配值1
- 为文件名分配值2
- 保存文件
不幸的是,遵循此顺序意味着在步骤 2) 中,FileName1
的值是一个空字符串,""
,因为它尚未被赋值。
因此,您应该遵循的顺序是:
- 定义变量
- 为文件名分配值1
- 为文件名分配值2
- 为路径赋值
- 保存文件
字符串中的其他变量
@Davesexcel 临时发布了一个答案(然后更改),假设字符串中的folder1
和folder2
也是变量。如果真的是这样,我附上了替代代码。
原始代码
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Path = "D:folder1folder2ProjectsThe FILEStheFILES"FileName1"
FileName1 = Range("B6")
FileName2 = Range("A1")
ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
更正的代码
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
FileName1 = Range("B6")
FileName2 = Range("A1")
Path = "D:folder1folder2ProjectsThe FILEStheFILES" & FileName1 & ""
ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
替代代码
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
FileName1 = Range("B6")
FileName2 = Range("A1")
Path = "D:" & folder1 & "" & folder2 & "ProjectsThe FILEStheFILES" & FileName1 & ""
ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
你去掉了一些与号,你还必须在使用变量之前分配变量的值:
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
FileName1 = Range("B6").Value
FileName2 = Range("A1").Value
Path = "D:folder1folder2ProjectsThe FILEStheFILES" & FileName1 & ""
ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
代码逐行运行,您需要先有正确的行。
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
FileName1 = Range("B6").Value
FileName2 = Range("A1")
Path = "D:folder1folder2ProjectsThe FILEStheFILES" & FileName1 & ""
这一行是错误的: 路径 ="D:\文件夹1\文件夹2\项目\文件\文件\"文件名 1"\
。因为它包含 3 英寸标记。我不确定您的实际路径应该是什么,因为操作系统中的路径名无论如何都不会包含" 字符。但是,在 VBA 字符串赋值中,字符串必须仅括在两个 " 字符之间:
"像这样">
如果您需要在字符串中逐字包含" 字符,则必须将引号加倍。因此,例如,要将字符串分配给读取"此字符串包含"字符"的变量,您需要编写:
Str = "This string contains a "" character"
注意加倍。
请检查字符串,然后重试。