在保存事件之前,需要帮助同时循环两个单元格
before save event, need help for looping two cells at the same time
我正在尝试执行保存前事件,如果两个给定单元格之一为空,则不允许用户保存。到目前为止我设法做的是链接第 13 列 (M) 和单元格 A4。
我想做的是将事件应用于两个范围和行的组合,A4-A19 和 M4-M19。这样:如果A4不为空且M4为空,则出现一个消息框并阻止保存等等..A5-M5,A6-M6 ...直到A19-M19。如果两个相应的单元格同时为空,则应该可以保存。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim i As Integer, MyWb As Object
i = 13
Set MyWb = ThisWorkbook.Sheets("Planning").Cells
Do While MyWb(4, i).Value <> ""
i = i + 1
Loop
If i = 13 Then
If ThisWorkbook.Sheets("Planning").Range("A4") <> "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
End If
End Sub
根据 Wolfie 的代码,我设法得到了我想要的,只是为 A 列添加一个 If not isempty 并将 19 替换为 13。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim plansht As Worksheet
Set plansht = ThisWorkbook.Sheets("Planning")
' Loop over rows
Dim rw As Integer
For rw = 4 To 19
' Test if both the A and M column in row "rw" are blank
If Not IsEmpty(plansht.Range("A" & rw)) And plansht.Range("M" & rw).Value = "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
Next rw
End Sub
试一试:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Const FIRST_ROW as integer = 4
Const LAST_ROW as integer = 19
Const ColA As Integer = 4
Const ColM as integer = 13
Dim MyWb As Worksheet
Dim CurRow as Integer
Set MyWb = ThisWorkbook.Sheets("Planning")
For CurRow = FIRST_ROW to LAST_ROW
If len(mywb.cells(CurRow, ColA)) = 0 and len(mywb.cells(CurRow, ColM)) = 0 then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
Exit For
End If
Next
End Sub
未经测试的代码(我永远记不起它是 (Row,Col) 还是 (Col,Row)),但我认为这会让您得到您想要的。这将中止对第一对都是空白的保存。
如果这行得通,您可以花点时间突出显示空白对,删除 OK 对的突出显示(在如果它们之前被突出显示),处理所有对并删除 Exit For
并提供一条错误消息,指出任何突出显示的内容都需要处理。
我听说 Len(string) = 0
比 string = vbNullString
更快地确定字符串是否为空。不能保证是否适合用途,但这是我了解到的。
试试这个:
For i = 4 to 19
If ThisWorkbook.Sheets("Planning").Range("A" & i) <> "" AND _
ThisWorkbook.Sheets("Planning").Range("M" & i) <> "" Then
MsgBox("Hey bro you didn't wrote properly on line " & i)
Cancel = True
Next i
您可以遍历行,只测试 A
和 M
列以测试给定行中它们是否均为空白。看下面的代码...
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim plansht as Worksheet
Set plansht = ThisWorkbook.Sheets("Planning")
' Loop over rows
Dim rw as Integer
For rw = 4 to 13
' Test if both the A and M column in row "rw" are blank
If plansht.Range("A" & rw).Value = "" And plansht.Range("M" & rw).Value = "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
Next rw
End Sub
编辑:
您的编辑建议您希望一些不同的单元格组合为空。以下是 If
语句针对不同结果
的一些示例
' If BOTH are empty
If plansht.Range("A" & rw).Value = "" And plansht.Range("M" & rw).Value = "" Then ...
If IsEmpty(plansht.Range("A" & rw)) And IsEmpty(plansht.Range("M" & rw)) Then ...
' If EITHER is empty
If plansht.Range("A" & rw).Value = "" OR plansht.Range("M" & rw).Value = "" Then ...
If IsEmpty(plansht.Range("A" & rw)) Or IsEmpty(plansht.Range("M" & rw)) Then ...
' If BOTH are NOT empty
If plansht.Range("A" & rw).Value <> "" And plansht.Range("M" & rw).Value <> "" Then ...
If Not IsEmpty(plansht.Range("A" & rw)) And Not IsEmpty(plansht.Range("M" & rw)) Then ...
请注意,当您开始引入具有多个条件的 Not
时,逻辑很快就会变得难以解释。您可以使用方括号将条件与 Not
分组,但您会得到这样的意思,逻辑上相同:
If Not IsEmpty(plansht.Range("A" & rw)) And Not IsEmpty(plansht.Range("M" & rw)) Then ...
If Not (IsEmpty(plansht.Range("A" & rw)) Or IsEmpty(plansht.Range("M" & rw))) Then ...
我正在尝试执行保存前事件,如果两个给定单元格之一为空,则不允许用户保存。到目前为止我设法做的是链接第 13 列 (M) 和单元格 A4。
我想做的是将事件应用于两个范围和行的组合,A4-A19 和 M4-M19。这样:如果A4不为空且M4为空,则出现一个消息框并阻止保存等等..A5-M5,A6-M6 ...直到A19-M19。如果两个相应的单元格同时为空,则应该可以保存。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim i As Integer, MyWb As Object
i = 13
Set MyWb = ThisWorkbook.Sheets("Planning").Cells
Do While MyWb(4, i).Value <> ""
i = i + 1
Loop
If i = 13 Then
If ThisWorkbook.Sheets("Planning").Range("A4") <> "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
End If
End Sub
根据 Wolfie 的代码,我设法得到了我想要的,只是为 A 列添加一个 If not isempty 并将 19 替换为 13。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim plansht As Worksheet
Set plansht = ThisWorkbook.Sheets("Planning")
' Loop over rows
Dim rw As Integer
For rw = 4 To 19
' Test if both the A and M column in row "rw" are blank
If Not IsEmpty(plansht.Range("A" & rw)) And plansht.Range("M" & rw).Value = "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
Next rw
End Sub
试一试:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Const FIRST_ROW as integer = 4
Const LAST_ROW as integer = 19
Const ColA As Integer = 4
Const ColM as integer = 13
Dim MyWb As Worksheet
Dim CurRow as Integer
Set MyWb = ThisWorkbook.Sheets("Planning")
For CurRow = FIRST_ROW to LAST_ROW
If len(mywb.cells(CurRow, ColA)) = 0 and len(mywb.cells(CurRow, ColM)) = 0 then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
Exit For
End If
Next
End Sub
未经测试的代码(我永远记不起它是 (Row,Col) 还是 (Col,Row)),但我认为这会让您得到您想要的。这将中止对第一对都是空白的保存。
如果这行得通,您可以花点时间突出显示空白对,删除 OK 对的突出显示(在如果它们之前被突出显示),处理所有对并删除 Exit For
并提供一条错误消息,指出任何突出显示的内容都需要处理。
我听说 Len(string) = 0
比 string = vbNullString
更快地确定字符串是否为空。不能保证是否适合用途,但这是我了解到的。
试试这个:
For i = 4 to 19
If ThisWorkbook.Sheets("Planning").Range("A" & i) <> "" AND _
ThisWorkbook.Sheets("Planning").Range("M" & i) <> "" Then
MsgBox("Hey bro you didn't wrote properly on line " & i)
Cancel = True
Next i
您可以遍历行,只测试 A
和 M
列以测试给定行中它们是否均为空白。看下面的代码...
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim plansht as Worksheet
Set plansht = ThisWorkbook.Sheets("Planning")
' Loop over rows
Dim rw as Integer
For rw = 4 to 13
' Test if both the A and M column in row "rw" are blank
If plansht.Range("A" & rw).Value = "" And plansht.Range("M" & rw).Value = "" Then
MsgBox ("You will need to enter topics before saving"), vbCritical
Cancel = True
End If
Next rw
End Sub
编辑:
您的编辑建议您希望一些不同的单元格组合为空。以下是 If
语句针对不同结果
' If BOTH are empty
If plansht.Range("A" & rw).Value = "" And plansht.Range("M" & rw).Value = "" Then ...
If IsEmpty(plansht.Range("A" & rw)) And IsEmpty(plansht.Range("M" & rw)) Then ...
' If EITHER is empty
If plansht.Range("A" & rw).Value = "" OR plansht.Range("M" & rw).Value = "" Then ...
If IsEmpty(plansht.Range("A" & rw)) Or IsEmpty(plansht.Range("M" & rw)) Then ...
' If BOTH are NOT empty
If plansht.Range("A" & rw).Value <> "" And plansht.Range("M" & rw).Value <> "" Then ...
If Not IsEmpty(plansht.Range("A" & rw)) And Not IsEmpty(plansht.Range("M" & rw)) Then ...
请注意,当您开始引入具有多个条件的 Not
时,逻辑很快就会变得难以解释。您可以使用方括号将条件与 Not
分组,但您会得到这样的意思,逻辑上相同:
If Not IsEmpty(plansht.Range("A" & rw)) And Not IsEmpty(plansht.Range("M" & rw)) Then ...
If Not (IsEmpty(plansht.Range("A" & rw)) Or IsEmpty(plansht.Range("M" & rw))) Then ...