Error vba for checking if required fields are not empty.

Phluke

New Member
Joined
Nov 1, 2018
Messages
3
Hi, I'm looking for vba code to check if column A has a value then column C must also have a value. I would like it to prompt me before saving.

Basically its a supplier list and if a supplier name is listed in column A then the commodity they provide must be filled in column C to comply with our operational standards.

I've tried a few different methods I've found online but most are for single cell checks.

Thanks!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi. Try something like this placed in the ThisWorkbook module:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim flag as Boolean, lr as Long, i as long, arr

flag = False

With Sheets("Sheet1")
    lr = .Cells(.Rows.Count, "A").End(xlUp).Row
    arr = .Range("A1:C" & lr)
    For i = LBound(arr, 1) To UBound(arr, 1)
        If Len(arr(i, 1)) > 0 Then
            If Len(arr(i, 3)) = 0 Then
                flag = True
                Exit For
            End If
        End If
    Next
End With

If flag = True Then
    SaveAsUI = False
    Cancel = True
    MsgBox "Fill in all fields"
End If
    
End Sub
 
Upvote 0
Hi. Try something like this placed in the ThisWorkbook module:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim flag as Boolean, lr as Long, i as long, arr

flag = False

With Sheets("Sheet1")
    lr = .Cells(.Rows.Count, "A").End(xlUp).Row
    arr = .Range("A1:C" & lr)
    For i = LBound(arr, 1) To UBound(arr, 1)
        If Len(arr(i, 1)) > 0 Then
            If Len(arr(i, 3)) = 0 Then
                flag = True
                Exit For
            End If
        End If
    Next
End With

If flag = True Then
    SaveAsUI = False
    Cancel = True
    MsgBox "Fill in all fields"
End If
    
End Sub

Thanks, I'm getting into this now I'll let you know what happens!
 
Upvote 0
This seems to be working perfectly, doing some more testing but you sir are a life saver, Thank you steve the fish!
 
Upvote 0

Forum statistics

Threads
1,216,185
Messages
6,129,383
Members
449,506
Latest member
nomvula

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top