如何在路径中写一个带变量的vba

时间:2018-05-16 16:12:28

标签: excel vba excel-vba variables path

这是我在这个网站上的第二篇文章,我对VBA来说相对较新。

我今天的问题是,

如何将单元格值添加到Path String以指定我希望保存工作簿的文件夹。

Dim Path As String
Dim FileName1 As String
Dim FileName2 As String

Path = "D:\folder1\folder2\Projects\The FILES\theFILES\"FileName1"\

FileName1 = Range("B6")
FileName2 = Range("A1")

ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

提前致谢!

5 个答案:

答案 0 :(得分:4)

回答陈述的问题

Path = "D:\folder1\folder2\Projects\The FILES\theFILES\" & 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. 将值分配给路径
  3. 将值分配给FileName1
  4. 将值分配给FileName2
  5. 保存文件
  6. 不幸的是,遵循此顺序意味着在步骤2),FileName1的值为空字符串"",因为尚未为其分配值。

    因此,您应遵循的顺序是:

    1. 定义变量
    2. 将值分配给FileName1
    3. 将值分配给FileName2
    4. 将值分配给路径
    5. 保存文件
    6. 字符串中的其他变量

      @Davesexcel暂时发布了一个答案(然后更改了),假设您的字符串中的folder1folder2也是变量。如果确实如此,我附上了 替代代码

      原始代码

      Dim Path As String
      Dim FileName1 As String
      Dim FileName2 As String
      
      Path = "D:\folder1\folder2\Projects\The FILES\theFILES\"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:\folder1\folder2\Projects\The FILES\theFILES\" & 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 & "\Projects\The FILES\theFILES\" & FileName1 & "\"    
      
      ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
      

答案 1 :(得分:3)

你删除了一些&符号,你也必须在使用之前指定变量的值:

Dim Path As String
Dim FileName1 As String
Dim FileName2 As String

FileName1 = Range("B6").Value
FileName2 = Range("A1").Value

Path = "D:\folder1\folder2\Projects\The FILES\theFILES\" & FileName1 & "\"

ActiveWorkbook.SaveAs Filename:=Path & FileName2 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

答案 2 :(得分:0)

代码逐行运行,您需要先拥有正确的行。

Dim Path As String
Dim FileName1 As String
Dim FileName2 As String

FileName1 = Range("B6").Value
FileName2 = Range("A1")
 Path = "D:\folder1\folder2\Projects\The FILES\theFILES\" & FileName1 & "\"

答案 3 :(得分:0)

这一行错了:     Path =“D:\ folder1 \ folder2 \ Projects \ The FILES \ theFILES \”FileName1“\

...因为它包含3个“标记。我不确定你的实际路径应该是什么,因为操作系统中的路径名称不包括”字符。但是,在VBA字符串赋值中,字符串必须仅包含在两个“字符:

之间

“喜欢这个”

如果你需要在字符串中包含“字符”,那么你必须加倍引用。所以例如为一个变量分配一个字符串'这个字符串包含一个“字符',你需要写:

Str = "This string contains a "" character"

注意加倍。

检查字符串,然后重试。

答案 4 :(得分:0)

你的行看起来更像是这样:

Path = "D:\folder1\folder2\Projects\The FILES\theFILES\" & FileName1 & "\"

以下是我的一个例子:

以下是输入:

https://i.stack.imgur.com/7TwhP.png

这是我正在保存的文件夹:

https://i.stack.imgur.com/y2bOZ.png

Public Sub saveFile()

Dim path As String
Dim nameOfPath As String
Dim nameOfFile As String

nameOfFile = Cells(2, 1)
nameOfPath = Cells(1, 1)
path = "Z:\" & nameOfPath

ActiveWorkbook.SaveAs filename:=path & nameOfFile & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled

End Sub