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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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