Excel宏报错‘Variable not defined’:跨工作表调用Sheet1生成PDF求助
问题:按钮移至Sheet2后VBA宏触发“变量未定义”错误
我正在开发一款可自动将Excel工作表信息填充至PDF的工作簿。把原Sheet1中的操作按钮移到Sheet2后,运行生成PDF的宏时触发了变量未定义错误,Sheet1被高亮标记。我需要借助Sheet1的数据在Sheet2生成PDF文档,相关代码如下,恳请协助解决:
原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
相关产品推荐
相关产品推荐

