Creating a save button with If statement.

Streetsweeper

New Member
Joined
May 7, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a save button on a user form that checks cells on sheet are not empty and saves.
The required cells for save will change daily to match the current date.
I use the f=application match to find the column that matches today’s date,
then Worksheet.functioncount to see if the column fields are completed.

Currently all this code is housed under the userform.
But after some reading it sounds like the I need a 2nd sub called Workbook_BeforSave and that needs to be stored under ThisWorkbook. I’ve tried this approach but no luck either.

Private Sub SaveButton_Click(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim rng As Range, f As Variant, col&

Set rng = Sheets(“QLDailySheet”).Range(“C4:AG4”)
f = Application.Match(CLng(Date), rng, 0)
If Not IsError(f) Then
col = rng(f).Column
Else
Msgbox “Today’s date not found”
Exit Sub

If WorksheetFunction.CountA(Worksheets(“QLDailySheet”).Cells(6,7,8,9,10,11,12,13,15,16,17,18, col)) > 12 Then
Msgbox “—> Save Aborted, “ vbCrLf & “—> All required fields must be completed.”
Cancel = True
Application.
End If
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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