我正在制作一个模板供用户填写,但他们不太擅长excel。我已经将VBA检查合并到它们上,单击“保存”以确保数据完整性和格式正确,但是反过来,我也不会过分地了解VBA!
如果没有满足,则前几个If终止保存,但是最后一个If则存在问题,这更多地是一个警告“如果您输入的这个百分比小于1,您要继续吗? %”,因为在某些情况下,他们可能希望这么做。如果单击“是”,则文档将保存,从而使其他检查变得多余。
我正在使用以下代码:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Dim lngstatrows As Long
'Checks if service consoles column has any zeros (If a site has been stated, they cannot have 0 consoles)
If Lookup.[v1] > 0 Then
MsgBox "ERROR: Sites are not allowed to have 0 consoles in Column O. DOCUMENT NOT SAVED"
Cancel = True
End If
'Checks if all required fields have data in them, flags message box if not.
If WorksheetFunction.CountA(DataInput.[B12:B1000]) <> (WorksheetFunction.CountA(DataInput.[c12:k1000]) + WorksheetFunction.CountA(DataInput.[M12:p1000])) / 13 Then
MsgBox "ERROR: Required field has data missing, please check Columns B - P. DOCUMENT NOT SAVED"
Cancel = True
End If
'Checks if Additive has 3 required fields enterred
If DataInput.[B3] = "Additive" And WorksheetFunction.CountA(DataInput.[B4:B6]) < 3 Then
MsgBox "ERROR: If pricing is Additive, please populate Cells B4 - B6. DOCUMENT NOT SAVED"
Cancel = True
End If
'Checks if Rebate% is less than 1% and asks for continue
If DataInput.[B7] <> "" And DataInput.[B7] < 1 Then
a = MsgBox("Rebate % is less than 1% - Are you sure you wish to proceed?", vbYesNo)
If a = vbYes Then
End If
End If
End Sub
所需结果:
将“终止保存” IF嵌套在“您是否要继续” IF中,以便他们单击“是”,模板仍必须通过其他检查,而这些检查将直接取消保存。 (我尝试自己做过,但是没有用,现在我在这里!)
答案 0 :(得分:0)
将您的If … Then … End If
状态语句转换为一个If … Then … ElseIf …
语句。
这意味着如果前三个条件之一为True
,它将取消保存。如果前3个是False
,而4个是True
,则询问他们是否要继续。如果他们不弹奏是,它将取消保存。
我所有这些条件都是False
,它不会取消,只会保存。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim lngstatrows As Long
If Lookup.[v1] > 0 Then
'Checks if service consoles column has any zeros (If a site has been stated, they cannot have 0 consoles)
MsgBox "ERROR: Sites are not allowed to have 0 consoles in Column O. DOCUMENT NOT SAVED"
Cancel = True
ElseIf WorksheetFunction.CountA(DataInput.[B12:B1000]) <> (WorksheetFunction.CountA(DataInput.[c12:k1000]) + WorksheetFunction.CountA(DataInput.[M12:p1000])) / 13 Then
'Checks if all required fields have data in them, flags message box if not.
MsgBox "ERROR: Required field has data missing, please check Columns B - P. DOCUMENT NOT SAVED"
Cancel = True
ElseIf DataInput.[B3] = "Additive" And WorksheetFunction.CountA(DataInput.[B4:B6]) < 3 Then
'Checks if Additive has 3 required fields enterred
MsgBox "ERROR: If pricing is Additive, please populate Cells B4 - B6. DOCUMENT NOT SAVED"
Cancel = True
ElseIf DataInput.[B7] <> "" And DataInput.[B7] < 1 Then
'Checks if Rebate% is less than 1% and asks for continue
a = MsgBox("Rebate % is less than 1% - Are you sure you wish to proceed?", vbYesNo)
'if they don't click yes then cancel
If a <> vbYes Then
Cancel = True
End If
End If
End Sub
或者,您可以将所有错误合并为一封邮件。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim CombinedErrors As String
'Checks if service consoles column has any zeros (If a site has been stated, they cannot have 0 consoles)
If Lookup.[v1] > 0 Then
CombinedErrors = CombinedErrors & "ERROR: Sites are not allowed to have 0 consoles in Column O." & vbCrLf
End If
'Checks if all required fields have data in them, flags message box if not.
If WorksheetFunction.CountA(DataInput.[B12:B1000]) <> (WorksheetFunction.CountA(DataInput.[c12:k1000]) + WorksheetFunction.CountA(DataInput.[M12:p1000])) / 13 Then
CombinedErrors = CombinedErrors & "ERROR: Required field has data missing, please check Columns B - P." & vbCrLf
End If
'Checks if Additive has 3 required fields enterred
If DataInput.[B3] = "Additive" And WorksheetFunction.CountA(DataInput.[B4:B6]) < 3 Then
CombinedErrors = CombinedErrors & "ERROR: If pricing is Additive, please populate Cells B4 - B6." & vbCrLf
End If
'if errors then cancel
If CombinedErrors <> vbNullString Then
MsgBox CombinedErrors & "DOCUMENT NOT SAVED"
Cancel = True
ElseIf DataInput.[B7] <> "" And DataInput.[B7] < 1 Then
'Checks if Rebate% is less than 1% and asks for continue
a = MsgBox("Rebate % is less than 1% - Are you sure you wish to proceed?", vbYesNo)
'if they don't click yes then cancel
If a <> vbYes Then
Cancel = True
End If
End If
End Sub
答案 1 :(得分:0)
您在我之前有一个答案,但是我对您的要求的理解略有不同,这就是我修改您的代码的目的。
希望我能完全理解您的要求。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim lngstatrows As Long
'Checks if Rebate% is less than 1% and asks for continue
If DataInput.[B7] <> "" And DataInput.[B7] < 1 Then
a = MsgBox("Rebate % is less than 1% - Are you sure you wish to proceed?", vbYesNo)
If a <> vbYes Then
Cancel = True
End If
End If
If Not Cancel Then
'Checks if service consoles column has any zeros (If a site has been stated, they cannot have 0 consoles)
If Lookup.[v1] > 0 Then
MsgBox "ERROR: Sites are not allowed to have 0 consoles in Column O. DOCUMENT NOT SAVED"
Cancel = True
End If
'Checks if all required fields have data in them, flags message box if not.
If WorksheetFunction.CountA(DataInput.[B12:B1000]) <> (WorksheetFunction.CountA(DataInput.[c12:k1000]) + WorksheetFunction.CountA(DataInput.[M12:p1000])) / 13 Then
MsgBox "ERROR: Required field has data missing, please check Columns B - P. DOCUMENT NOT SAVED"
Cancel = True
End If
'Checks if Additive has 3 required fields enterred
If DataInput.[B3] = "Additive" And WorksheetFunction.CountA(DataInput.[B4:B6]) < 3 Then
MsgBox "ERROR: If pricing is Additive, please populate Cells B4 - B6. DOCUMENT NOT SAVED"
Cancel = True
End If
End If
End Sub