0

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
1
  • This seems to do what you want.....What is the issue?? Commented Feb 1, 2015 at 19:48

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man,dots before Cells(Rows.Count, "A") and .Range("A" & i & ":I" & i) has to be removed then works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.