数据验证宏excel VBA

时间:2015-02-01 19:35:08

标签: excel vba excel-vba

我试图创建一个宏来检查用户是否填充了他的行。 例如,如果用户开始在单元格A1中键入,则宏检查单元格是否填充在同一行上,如果空白用户在填充所有空白之前无法关闭excel。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rsave As Range
Dim cell As Range
Set rsave = Sheet1.Range("a1:i1")
For Each cell In rsave
If cell = "" Then
    Dim missdata
    missdata = MsgBox("missing data", vbOKOnly, "Missing Data")
    Cancel = True
    cell.Select  
    Exit For
   End If
  Next cell
End Sub

1 个答案:

答案 0 :(得分:1)

要为多个行执行此操作,请使用:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rsave As Range, N As Long
Dim cell As Range
With Sheet1
    N = .Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To N
        Set rsave = .Range("A" & i & ":I" & i)
        For Each cell In rsave
            If cell = "" Then
                Dim missdata
                missdata = MsgBox("missing data", vbOKOnly, "Missing Data")
                Cancel = True
                cell.Select
                Exit Sub
            End If
          Next cell
    Next i
End With
End Sub