Search code examples
excelvbaexcel-2016save-as

Trying to SaveAs excel workbook using VBA and specific network path


I'm trying to use VBA code to save an excel workbook with a specific file name based on cell data and in a specific network folder. Here's the code.

Private Sub CommandButton1_Click()
Dim Path As String
Dim FileName1 As String
Dim FileName2 As String
Path = "H:\testing folder\"
FileName1 = Range("A8")
FileName2 = Range("A11")
ActiveWorkbook.SaveAs Filename:=FileName1 & "_" & FileName2 & ".xlsx", FileFormat:=51
End Sub

the file is just being saved in the H drive and not the testing folder in the H drive. Also, the activeworkbook line did have Filename:=Path & FileName1 etc. but it was saving in the same place with the name of the path end folder in front of "FileName1".


Solution

  • Private Sub CommandButton1_Click()
    Dim Path As String
    Dim FileName1 As String
    Dim FileName2 As String
    Path = "H:\testing folder\"
    FileName1 = Range("A8")
    FileName2 = Range("A11")
    ActiveWorkbook.SaveAs Filename:=Path & FileName1 & "_" & FileName2 & ".xlsx", FileFormat:=51
    End Sub