我是VBA的新手,作为我自己开发的一部分,我目前正在重新编写自己写的一些自动化技术,这些自动化技术已在大约6个月前完成。我有以下代码,该代码根据按下按钮的工作表来标识工作表名称(有两个不同的工作表名称,类似于CustName_CALC
和两个按钮)。然后,它遍历工作簿中的其余工作表以查找“相似”工作表。
以下子项PDF_Bill()
适用于上述按钮:
Public vPDFilename As Variant
Public wb As Workbook
Public ws As Worksheet
Public wsBill As Worksheet
Public wsCalc As Worksheet
Public wsStatement As Worksheet
Public sLocation As String, sCustName As String, myTitle As String, myMsg As String, InitialFileName As String, Response As String, C_Response As String
Sub PDF_Bill()
sCustName = Left(ActiveSheet.Name, InStr(ActiveSheet.Name, "_") - 1)
Set wb = ThisWorkbook
Set wsCalc = wb.Sheets(sCustName & "_CALC")
myTitle = "Save Invoice"
myMsg = "Are you sure you would like to save the " & wsCalc.Cells(1, 2).Value2 & " invoice?"
Response = MsgBox(myMsg, vbQuestion + vbOKCancel, myTitle)
Select Case Response
Case Is = vbOK
For Each wsBill In ThisWorkbook.Worksheets
If wsBill.Name Like sCustName & "_BILL" & "*" Then
Call Module1_PDF.PDF_Procedure
End If
Next wsBill
Case Is = vbCancel
'user cancels the first popup message
myTitle = "Invoice Cancelled!"
myMsg = "You've cancelled the request to save the invoice!"
C_Response = MsgBox(myMsg, vbOKOnly, myTitle)
End Select
End Sub
上面这样称呼:
Sub PDF_Procedure()
sCustName = Left(ActiveSheet.Name, InStr(ActiveSheet.Name, "_") - 1)
sLocation = "S:DRIVELOCATION" & wsCalc.Cells(1, 2).Value2 & "\Invoices\"
vPDFilename = Application.GetSaveAsFilename( _
InitialFileName:=sLocation _
& wsCalc.Cells(1, 2).Value2 _
& " " _
& MonthName(Month(Date)) _
& " Invoice", _
FileFilter:="PDF, *.pdf", _
Title:="Save as PDF")
If vPDFilename <> False Then
With wsBill
'.Visible = xlSheetVisible '[will be hidden at later state]
'.Activate
'predefined area for 5 page invoice - this may need to change in future.
.PageSetup.PrintArea = "A1:S300"
End With
'creates the PDF
wsBill.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=vPDFilename, _
OpenAfterPublish:=False
'wsBill.Visible = xlSheetHidden 'bill sheet HIDE [WILL BE USED LATER NOT SET UP]
Else
'if user cancels save dialog box
myTitle = "Invoice Cancelled!"
myMsg = "You've cancelled the request to save the invoice!"
C_Response = MsgBox(myMsg, vbOKOnly, myTitle)
wsCalc.Activate
GoTo CancelProcess
End If
CancelProcess:
Exit Sub
End Sub
上面的代码(除了,GoTo CancelProcess
因为这是我一直在努力解决的问题)对于一张称为的工作表非常适用,CustName_Bill
但是我还需要它在以下情况下工作。
我已经审查了以下页面,但没有成功:
我知道我需要在exit
某处声明,但是我一直在努力理解它的去向。我看到的许多示例不够复杂,不足以使我得出如何解决问题的结论。
我目前的问题是,我有两张纸被命名为相似(故意):
循环继续到下一个工作表,即CustName_Bill_Type1
当用户取消时,Application.GetSaveAsFilename
但是我需要for each
在用户取消第一个 Application.GetSaveAsFilename
对话框时退出循环。
所以我的问题是,exit
一旦用户在第一个保存对话框上单击“取消”,我就需要把代码完全终止的位置放在哪里。
您需要将子项转换为函数,以便在用户取消保存后返回FALSE。
Function PDF_Procedure() as Boolean
sCustName = Left(ActiveSheet.Name, InStr(ActiveSheet.Name, "_") - 1)
sLocation = "S:DRIVELOCATION" & wsCalc.Cells(1, 2).Value2 & "\Invoices\"
vPDFilename = Application.GetSaveAsFilename( _
InitialFileName:=sLocation _
& wsCalc.Cells(1, 2).Value2 _
& " " _
& MonthName(Month(Date)) _
& " Invoice", _
FileFilter:="PDF, *.pdf", _
Title:="Save as PDF")
If vPDFilename <> False Then
With wsBill
'.Visible = xlSheetVisible '[will be hidden at later state]
'.Activate
'predefined area for 5 page invoice - this may need to change in future.
.PageSetup.PrintArea = "A1:S300"
End With
'creates the PDF
wsBill.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=vPDFilename, _
OpenAfterPublish:=False
' return true if saved
PDF_Procedure = True
'wsBill.Visible = xlSheetHidden 'bill sheet HIDE [WILL BE USED LATER NOT SET UP]
Else
'if user cancels save dialog box
myTitle = "Invoice Cancelled!"
myMsg = "You've cancelled the request to save the invoice!"
C_Response = MsgBox(myMsg, vbOKOnly, myTitle)
wsCalc.Activate
'return false if cancelled
PDF_Procedure = False
End If
End Function
并向您的PDF_Bill子项添加条件
For Each wsBill In ThisWorkbook.Worksheets
If wsBill.Name Like sCustName & "_BILL" & "*" Then
If Module1_PDF.PDF_Procedure() = False Then
Exit sub
End if
End If
Next wsBill
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句