简体   繁体   中英

Data validation macro excel VBA

im trying to create a macro which checks if user has filled his row. For example, if user starts typing in cell A1, macro checks if the cells are filled on the same row if theres a blank user cant close excel before all blanks are filled.

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

To perform this for more than one row, use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM