我在A11列中使用Excel创建了一个表单我有一个带有三个选项的下拉框。如果选择了其中一个选项,则需要整行。我到处搜索,无法找到合适的代码。我是使用VBA的新手
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rsave As Range
Dim cell As Range
Set rsave = Sheet1.Range("B11)]")
For Each cell In rsave
If cell.Value <> "" And cell.Offset(0, 1).Value = "" Then
Dim missdata
missdata = MsgBox("Missing Data - Enter the Date for WorkBook to Save", vbOKOnly, "Missing Data")
Cancel = True
cell.Offset(0, 1).Select
Exit For
End If
Next cell
End Sub
答案 0 :(得分:0)
这应该这样做。将“SheetToCheck”设置为您要检查的工作表。 它会在找到缺失值后立即停止循环,并且MsgBox会在第1个范围内找到缺失值。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
Dim lastrow As Integer
Dim CellToCheck As Range
Dim exitLoops As Boolean
Set ws = Worksheets("SheetToCheck")
lastrow = ws.UsedRange.Rows.Count
exitLoops = False
With ws
'Loop over rows:
For i = 2 To lastrow
If exitLoops = True Then Exit For
If .Range("A" & i).value <> "" And .Range("A" & i).value <> "SELECT ONE" Then
'Loop over cells in row:
For Each CellToCheck In .Range("B" & i & ":K" & i)
If exitLoops = True Then Exit For
If CellToCheck.value = "" Then
Cancel = True
MsgBox "Missing Data in cell " & CellToCheck.Address
exitLoops = True
End If
Next CellToCheck
End If
Next i
End With
End Sub