vba - 通过 vba 启用自动另存为(无需在提示时单击“保存”)

标签 vba excel

enter image description here

如何让excel通过VBA自动选择保存,而不需要手动点击。例如-我有代码选择数据验证列表中的每个值并将我带到下面的阶段,尽管我每次都必须单击“保存”。

我尝试添加:

  Application.EnableEvents = True

但它仍然只带我到图像上的舞台。

    Sub PDFActiveSheet()
    Dim ws As Worksheet
    Dim strPath As String
    Dim myFile As Variant
    Dim strFile As String
    On Error GoTo errHandler

    Set ws = ActiveSheet

    'enter name and select folder for file
    ' start in current workbook folder
    strFile = Cells.Range("B1") & " Period " & Cells.Range("J1")

    strFile = ThisWorkbook.Path & "\" & strFile

    myFile = Application.GetSaveAsFilename _
         (InitialFileName:=strFile, _
            FileFilter:="PDF Files (*.pdf), *.pdf", _
            Title:="Select Folder and FileName to save")

    If myFile <> "False" Then
        ws.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False, _
        From:=1, _
        To:=2  
   End If   
    exitHandler:
        Exit Sub
    errHandler:
        MsgBox "Could not create PDF file"
        Resume exitHandler

    End Sub

最佳答案

为了避免提示:

Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler

Set ws = ActiveSheet

'enter name and select folder for file
' start in current workbook folder
strFile = Cells.Range("B1") & " Period " & Cells.Range("J1")

strFile = ThisWorkbook.Path & "\" & strFile & ".PDF"

    ws.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False, _
    From:=1, _
    To:=2

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler

End Sub

关于vba - 通过 vba 启用自动另存为(无需在提示时单击“保存”),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532997/

相关文章:

sql-server:我如何知道谁在我的数据库中?

arrays - 我可以从 ws.usedrange.value 返回一个以 0 为基数的数组吗?

vba - On Error Goto 在 EventHandler subs 中不起作用

javascript - 将转置 JSON 保存到 Excel 工作表

VBA,Excel - 比较多个条件的行,复制并在新工作表中添加结果

vba - 如何调整 32 位和 64 位 Excel VBA 日期处理之间的差异?

vba - 使 VBA 表单文本框仅接受数字(包括 +、- 和 .)

vb.net - 如何从创建的 "Excel.Application"对象中获取进程 ID?

php - MIME 类型 XLSX(来自 LibreOffice)

excel - Excel 工作表的单元格类型显示为数字,实际上是文本类型