VBA If cell is blank, then message box and exit sub, otherwise continue macro

Will85

Board Regular
Joined
Apr 26, 2012
Messages
240
Office Version
  1. 365
Platform
  1. Windows
I have what I thought was easy enough, but I am struggling with the syntax.

I want my macro to check if a cell is blank before running the rest of the code. If the cell is blank, then pop up a message box and stop the rest of the macro, if its not blank, then continue the macro.

I am not sure what to put between Exit Sub and My code for the macro I want to run.

If Worksheets("Instructions").Range("C3").Value = "" Then
MsgBox "Missing Date"
Exit Sub


My code
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You are pretty close. Something structured like this would work:
VBA Code:
Sub Macro1()

    If Worksheets("Instructions").Range("C3").Value = "" Then
        MsgBox "Missing Date"
        Exit Sub
    End If

    My code

End Sub
 
Upvote 0
Thank you, that appears to be working. I could have sworn I tried that, but it was still going through the rest of the code even though its blank. Possible I accidently was using an Else statement.

Thank you.
 
Upvote 0
Thank you, that appears to be working. I could have sworn I tried that, but it was still going through the rest of the code even though its blank. Possible I accidently was using an Else statement.

Thank you.
An else statement should work fine too, if structured correctly i.e.
VBA Code:
Sub Macro1()

    If Worksheets("Instructions").Range("C3").Value = "" Then
        MsgBox "Missing Date"
        Exit Sub
    Else
        My code
    End If

End Sub
Where you can get it to trouble is if C3 is not really blank.
If it is something like a single space, " " is not the same as "", so your code would interpret that as something.
 
Upvote 0
An else statement should work fine too, if structured correctly i.e.
VBA Code:
Sub Macro1()

    If Worksheets("Instructions").Range("C3").Value = "" Then
        MsgBox "Missing Date"
        Exit Sub
    Else
        My code
    End If

End Sub
Where you can get it to trouble is if C3 is not really blank.
If it is something like a single space, " " is not the same as "", so your code would interpret that as something.
Would there be a better way to structure it, so that it only accepts a true date. Right now I am just trying to make sure a user remembers to input something into the cell, but I suppose they could enter jibberish or mess up the date.

Is there a way for it to tell if the value is an actual date value?
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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