VBA for on save event

RPrasad

Board Regular
Joined
Mar 22, 2008
Messages
144
I have 5 columns in spreadsheet. User has to fill up 4 columns, if column A is filled up and any one of the other 3 columns(C,D or E) left blank in the blank cells it should print the message "Required Column". This event should occur when the user saves the spreadsheet. and message should appear on all cells where col A is not blank.

my previous post was similar to this but it was worksheetchange event and this requirement is completely different from that.
Please help.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Two events need to be updated.

1) Workbook_BeforeSave
2) Workbook_BeforeClose

Also this macro only looks for values in sheet1, not on all sheets. Hope this is what you are looking for.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim i As Integer
    i = 1
    Do While Worksheets(1).Cells(i, 1) <> ""
        If Worksheets(1).Cells(i, 2) = "" Or Worksheets(1).Cells(i, 3) = "" Or Worksheets(1).Cells(i, 4) = "" Then
            MsgBox ("Row number: " & i & " has not been filled up correctly")
            MsgBox ("Columns B,C and D cannot be blank for row:= " & i)
            Cancel = True
            Exit Sub
        End If
        i = i + 1
    Loop
End Sub
 
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim i As Integer
    i = 1
    Do While Worksheets(1).Cells(i, 1) <> ""
        If Worksheets(1).Cells(i, 2) = "" Or Worksheets(1).Cells(i, 3) = "" Or Worksheets(1).Cells(i, 4) = "" Then
            MsgBox ("Row number: " & i & " has not been filled up correctly")
            MsgBox ("Columns B,C and D cannot be blank for row:= " & i)
            Cancel = True
            Exit Sub
        End If
        i = i + 1
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,005
Members
449,480
Latest member
yesitisasport

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