组合 BeforeSave 事件的条件
Combining criteria for BeforeSave event
我需要一些有关 BeforeSave VBA 事件的帮助。
我引入了一个额外的标准,如果单元格不等于 10 个字符,则使用条件格式来突出显示它。
问题是,我在 VBA 中已经有一个 BeforeSave 事件来检查复选框是否被选中,我如何组合这两个语句以便它在保存之前检查这两个条件?
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1
Dim rng As Range
For Each rng In Worksheets(1).UsedRange
If rng.DisplayFormat.Interior.Color = vbRed Then
MsgBox ("Please correct any fields highlighted in red")
Exit For
End If
Next rng
Application.ScreenUpdating = True
If Cancel Then MsgBox "Please accept the terms and conditions before saving the Invoice"
End Sub
突出显示的标准是我用来评估复选框的标准,中间是我试图检查是否有任何填充为红色的单元格的代码。也是 excel sheet 中的示例。
感谢您的帮助!
你很接近!几个变化:
您需要检查单元格的 .DisplayFormat
,因为这是条件格式。
您在进入 If
状态之前退出了您的子例程。请改用 Exit For
。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1
Dim rng As Range
For Each rng In ActiveSheet.UsedRange
If rng.DisplayFormat.Interior.Color = vbRed Then
Cancel = True
Exit For
End If
Next rng
Application.ScreenUpdating = True
If Cancel Then MsgBox "Please accept the terms and conditions"
End Sub
另外 Application.ScreenUpdating = True
需要在你的循环之外,否则它可能永远不会被打开!
更新:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1
Dim rng As Range
For Each rng In Worksheets(1).UsedRange
If rng.DisplayFormat.Interior.Color = vbRed Then
MsgBox ("Please correct any fields highlighted in red")
Cancel = True
Application.ScreenUpdating = True
Exit Sub
End If
Next rng
Application.ScreenUpdating = True
If Cancel Then MsgBox "Please accept the terms and conditions before saving the Invoice"
End Sub
我需要一些有关 BeforeSave VBA 事件的帮助。
我引入了一个额外的标准,如果单元格不等于 10 个字符,则使用条件格式来突出显示它。
问题是,我在 VBA 中已经有一个 BeforeSave 事件来检查复选框是否被选中,我如何组合这两个语句以便它在保存之前检查这两个条件?
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1
Dim rng As Range
For Each rng In Worksheets(1).UsedRange
If rng.DisplayFormat.Interior.Color = vbRed Then
MsgBox ("Please correct any fields highlighted in red")
Exit For
End If
Next rng
Application.ScreenUpdating = True
If Cancel Then MsgBox "Please accept the terms and conditions before saving the Invoice"
End Sub
突出显示的标准是我用来评估复选框的标准,中间是我试图检查是否有任何填充为红色的单元格的代码。也是 excel sheet 中的示例。
感谢您的帮助!
你很接近!几个变化:
您需要检查单元格的
.DisplayFormat
,因为这是条件格式。您在进入
If
状态之前退出了您的子例程。请改用Exit For
。Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Application.ScreenUpdating = False Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1 Dim rng As Range For Each rng In ActiveSheet.UsedRange If rng.DisplayFormat.Interior.Color = vbRed Then Cancel = True Exit For End If Next rng Application.ScreenUpdating = True If Cancel Then MsgBox "Please accept the terms and conditions" End Sub
另外 Application.ScreenUpdating = True
需要在你的循环之外,否则它可能永远不会被打开!
更新:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = Not Worksheets(1).CheckBoxes(1).Value = 1
Dim rng As Range
For Each rng In Worksheets(1).UsedRange
If rng.DisplayFormat.Interior.Color = vbRed Then
MsgBox ("Please correct any fields highlighted in red")
Cancel = True
Application.ScreenUpdating = True
Exit Sub
End If
Next rng
Application.ScreenUpdating = True
If Cancel Then MsgBox "Please accept the terms and conditions before saving the Invoice"
End Sub