这是我的第一篇文章,如果我做错了,请耐心等待。 我的问题是: 我有两列,A是孩子的数量,B是那些孩子的名字。 这些值是手动输入的,我只想在A填充时强制使用B. 这就是我的想法:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not IsEmpty(Sheet1.Range("A1")) Then
If IsEmpty(Sheet1.Range("B1")) Then
MsgBox "Please fill in cell B1 before closing."
Cancel = True
Else '
End If
End If
End Sub
这实际上工作得很好,遗憾的是我无法将整个列扩展到它,当用A1代替A1:A1000和B1代替B1:B1000时,它不起作用。
如何对整个A列和B列进行验证?
提前感谢!
答案 0 :(得分:2)
试试这个
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = Evaluate("SUMPRODUCT(--(ISBLANK(Sheet1!B:B) <> ISBLANK(Sheet1!A:A)))")
If Cancel Then MsgBox "Please fill in column B before closing."
End Sub
修改强>
为了将用户带到缺少数据的地方,并考虑您提供的有关数据的其他信息,请尝试以下操作:
'Private Sub Workbook_BeforeClose(Cancel As Boolean)
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim r: r = Evaluate( _
"MATCH(FALSE, ISBLANK('ELENCO AGGIORNATO'!V:V) = ISBLANK('ELENCO AGGIORNATO'!W:W), 0)")
If IsError(r) Then Exit Sub ' All is fine
Cancel = True
Application.Goto Sheets("ELENCO AGGIORNATO").Cells(r, "V").Resize(, 2)
msgBox "Please fill missing data before saving."
End Sub
另请注意,我建议使用Workbook_BeforeSave
而不是Workbook_BeforeClose
,因为如果用户决定放弃他的(不完整)工作并关闭工作簿而不保存,则不会有任何损害。
答案 1 :(得分:0)
您可以尝试这样的事情......
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim str As String
Dim Rng As Range, Cell As Range
Dim FoundBlank As Boolean
Set Rng = Sheet1.Range("A1:A1000")
str = "Please fill the cells listed below before colsing..." & vbNewLine & vbNewLine
For Each Cell In Rng
If Cell <> "" And Cell.Offset(0, 1) = "" Then
FoundBlank = True
str = str & Cell.Address(0, 1) & vbNewLine
End If
Next Cell
If FoundBlank Then
Cancel = True
MsgBox str, vbExclamation, "List of Blank Cells Found!"
End If
End Sub