You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel宏报错‘Variable not defined’:跨工作表调用Sheet1生成PDF求助

问题:按钮移至Sheet2后VBA宏触发“变量未定义”错误

我正在开发一款可自动将Excel工作表信息填充至PDF的工作簿。把原Sheet1中的操作按钮移到Sheet2后,运行生成PDF的宏时触发了变量未定义错误,Sheet1被高亮标记。我需要借助Sheet1的数据在Sheet2生成PDF文档,相关代码如下,恳请协助解决:

原Sheet1中的操作按钮:
原Sheet1中的操作按钮

错误提示截图:
错误提示:变量未定义

相关VBA代码:

Option Explicit
Enum W32_Window_State
    Show_Normal = 1
    Show_Minimized = 2
    Show_Maximized = 3
    Show_Min_No_Active = 7
    Show_Default = 10
End Enum
 
Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hWnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
Function OpenURL(URL As String, WindowState As W32_Window_State) As Boolean
 
' Opens passed URL with default application, or Error Code (<32) upon error
 
    Dim lngHWnd As Long
    Dim lngReturn As Long
 
    lngReturn = ShellExecute(lngHWnd, "open", URL, vbNullString, _
        vbNullString, WindowState)
   
    OpenURL = (lngReturn > 32)
End Function
Sub PDFTemplate()
Dim PDFFldr As FileDialog
Set PDFFldr = Application.FileDialog(msoFileDialogFilePicker)
With PDFFldr
    .Title = "Select PDF file to attach'"
    .Filters.Add "PDF Type Files", "*.pdf", 1
    If .Show <> -1 Then GoTo NoSelection
    ActiveSheet.Range("D12").Value = .SelectedItems(1)
End With
NoSelection:
End Sub
Sub SavePDFFolder()
Dim PDFFldr As FileDialog
Set PDFFldr = Application.FileDialog(msoFileDialogFolderPicker)
With PDFFldr
    .Title = "Select a Folder"
    If .Show <> -1 Then GoTo NoSel:
    ActiveSheet.Range("D17").Value = .SelectedItems(1)
End With
NoSel:
End Sub
Sub CreatePDFForms()
Dim PDFTemplateFile, NewPDFName, SavePDFFolder, FirstName As String
Dim EplyrName As String
Dim CustRow, LastRow As Long


With Sheet1
    LastRow = .Range("E9999").End(xlUp).Row  'LastRow
    PDFTemplateFile = .Range("D12").Value 'Template File Name
    SavePDFFolder = .Range("D17").Value 'Save PDF Folder
    OpenURL "" & PDFTemplateFile & "", Show_Maximized
    Application.Wait Now + 0.00006

内容的提问来源于stack exchange,提问作者hakai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 14:00:33