基于预填充的单元格保存 excel 文件

Saving an excel file based on prepopulated cells

我制作了一个命令按钮,允许用户根据 excel 单元格中的值保存文件,其中单元格以 pre-populated 开头。另外,如何实现此功能 GetSaveAsFilename 以便用户可以选择保存目的地但不能更改标题。但是我在执行这段代码时遇到错误。

Private Sub CommandButton2_Click()
Sub SaveMyWorkbook()

    Dim strPath As String
    Dim strFolderPath As String

    strFolderPath = "C:\Users\"

    strPath = strFolderPath & _
        DoNotPrint - Setup.Range("C7").Value & " " & _
        DoNotPrint - Setup.Range("C8").Value & " " & _
        DoNotPrint - Setup.Range("C45").Value & " " & _
        DoNotPrint - Setup.Range("C9").Value & ".xlsm"

End Sub

最佳猜测:

With Thisworkbook.sheets("DoNotPrint - Setup")

    strPath = strFolderPath & .Range("C7").Value & " " & _
                              .Range("C8").Value & " " & _
                              .Range("C45").Value & " " & _
                              .Range("C9").Value & ".xlsm"

End with

正在选择要保存到的文件夹:

VBA EXCEL To Prompt User Response to Select Folder and Return the Path as String Variable

为了让用户选择我使用的文件夹:

Private Sub CommandButton2_Click()

    Dim strPath As String
    Dim strFolderPath As String

    strFolderPath = "C:\Users\"

    strPath = strFolderPath & _
        DoNotPrint - Setup.Range("C7").Value & " " & _
        DoNotPrint - Setup.Range("C8").Value & " " & _
        DoNotPrint - Setup.Range("C45").Value & " " & _
        DoNotPrint - Setup.Range("C9").Value & ".xlsm"

    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = strPath
        .FilterIndex = 2
        .Title = Place Title Here if you want
            If .Show = -1 Then .Execute
    End With
End Sub

根据 Tim 和 Zack 的回答,这有效

Private Sub CommandButton2_Click()
    Dim strPath As String
    Dim strFolderPath As String

    strFolderPath = "C:\Users\"


With ThisWorkbook.Sheets("DoNotPrint - Setup")

    strPath = strFolderPath & .Range("C7").Value & " " & _
                              .Range("C8").Value & " " & _
                              .Range("C45").Value & " " & _
                              .Range("C9").Value & ".xlsm"
    End With
    With Application.FileDialog(msoFileDialogSaveAs)
        .AllowMultiSelect = False
        .InitialFileName = strPath
        .FilterIndex = 2

            If .Show = -1 Then .Execute
    End With

End Sub