excel - 取消保存对话框时退出每个循环
问题描述
我是 VBA 的新手,作为我自己开发的一部分,我目前正在重写一些我在大约 6 个多月前完成的自动化。我有下面的代码,它根据按下按钮的工作表来识别工作表名称(有两个不同的工作表名称类似于CustName_CALC
和两个按钮)。然后它会遍历工作簿中的剩余工作表以查找“相似”工作表。
下面的 subPDF_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_Type
- CustName_Bill_Type1
循环继续到下一张纸,即CustName_Bill_Type1
当用户取消Application.GetSaveAsFilename
但我需要在用户取消第一个对话框for each
时退出循环。 Application.GetSaveAsFilename
exit
所以我的问题是,一旦用户在第一个保存对话框上单击取消,我需要将代码放在哪里才能完全终止。
解决方案
您需要将 sub 转换为函数,以便在用户取消保存时返回 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
推荐阅读
- python - Wait_for and buttons not working for discord bot
- django - 跨域请求被阻止:同源策略不允许读取远程资源...(原因:CORS 未成功)
- sql - 从表中选择具有最大日期的行
- git - 将 Homestead Vagrant VM 上的项目上传到 Github
- textbox - 实时日期分隔符
- swiftui - 关于从后台线程发布更改的 SwiftUI 错误
- google-drive-api - 无论如何要在不下载的情况下解压缩谷歌驱动器中的 zip 或 tar
- node.js - 为什么请求管道在此代码中包含标头?
- java - Java fizzBuzz(来自 CodingBat-Array2 的练习)
- tkinter - 如何从 Combobox 获取值以写入文本框