Excel VBA Cell Validation

SamByPublicDemand

New Member
Joined
Jun 7, 2012
Messages
11
Is there a way to check with VBA whether a certain cell is filled? I need a message box to pop up warning the user if cell B2 is not filled in before continuing with the rest of the macro, but I have no idea where to start, any help? Thank you in advance.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the board..

Try

Code:
If Range("B2") = "" Then
    MsgBox "You must enter data in B2"
    Exit Sub
End If
 
Upvote 0
Thanks.

Also is it possible to make it check for that cell in Sheet2 rather than the active sheet just incase someone runs the macro whilst not in Sheet2.
 
Upvote 0
Try

Code:
If Sheets("Sheet2").Range("B2") = "" Then
    MsgBox "You must enter data in Sheet2!B2"
    Exit Sub
End If
 
Upvote 0
Thank you, that works fine, is there a way to check multiple cells using the same code? Specifically B1, B18, B20 and B22 in sheet 2 are not empty, and check B1 in sheet 3 contains a certain string?
 
Upvote 0
Oh also, the rest of the code runs even after the message box comes up, here is the current code :

PHP:
Sub CheckMaster()

If Sheets("Sheet2").Range("B1") = "" Then
    MsgBox "You must enter data in Sheet2!B2"
    Exit Sub
End If
End Sub

If the cells are blank, then the code is not run, and if the cell is filled, it carries on.
 
Upvote 0
Try

<font face=Error: Key or Value Not Found.><SPAN style="color:#00007F">Sub</SPAN> CheckMaster()<br><br><SPAN style="color:#00007F">With</SPAN> Sheets("Sheet2")<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> .Range("B1, B18, B20, B22")<br>        <SPAN style="color:#00007F">If</SPAN> c = "" <SPAN style="color:#00007F">Then</SPAN><br>            MsgBox "You must enter data in Sheet2!" & c.Address<br>            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> c<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,207,256
Messages
6,077,317
Members
446,278
Latest member
hoangquan2310

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