Search code examples
exceldynamic-datamissing-databefore-savevba

VBA Check For Missing Data for Dymanic Range


I am stuck on figuring out how to make a dynamic range for this database page for a project to check if the Phase data is missing before saving the workbook. The database page will continue to grow and not sure how to make this range dynamic.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim rsave As Range
Dim cell As Range

Sheets("Database").Select

Set rsave = Range("a7:a125")

For Each cell In rsave
If cell = "" Then
    Dim missdata
    missdata = MsgBox("Missing Phase Data", vbOKOnly, "Missing Phase Data")
    Cancel = True
    cell.Select
    Exit For
End If
Next cell
End Sub

Solution

  • This will give you the full story on missing items:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim msg As String, N As Long, cl As Range
        N = Cells(Rows.Count, 1).End(xlUp).Row +1
        Dim r As Range
        Set r = Range("A7:A" & N)
        For Each cl In r
            If cl.Value = "" Then
                msg = msg & vbCrLf & cl.Address(0, 0)
            End If
        Next cl
        If msg = "" Then Exit Sub
            MsgBox msg
            Cancel = True
    End Sub