VBA to prevent macro if cells are not filled in.

tomexcel1

New Member
Joined
Feb 22, 2018
Messages
47
Hi All

Im using the below code to prevent a macro from running if all fields in a tabel are not filled in. I just cant seam to make it check a range of cells. the ranges i want checking are:

D8:D12
D14:G24
D26:D30
D32:D34

Can anyone Please help! Thanks in advance


Code:
Sub Check ()
If Range("G3") = Empty Then    MsgBox "Please fill in all fields!"
    Else
    MsgBox "OK"
    'Macro to run here
    End If
End Sub
 

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.
Here is one way

Code:
Sub Check()
    Dim RangesToCheck As Range, R As Range
    Set RangesToCheck = Application.Union(Range("D8:D12"), Range("D14:G24"), Range("D26:D30"), Range("D32:D34"))
    Debug.Print RangesToCheck.Address
    For Each R In RangesToCheck
        If R = "" Then
            MsgBox "Please fill in all fields!!", Buttons:=vbOKOnly + vbExclamation, Title:="Input Data Check"
        End If
        Exit Sub
    Next R
End Sub

Also, here is a very similar thread

https://www.mrexcel.com/forum/excel-questions/1050709-i-have-marco-i-need-some-help.html
 
Upvote 0
Also possible:

Code:
If [COUNTA(D8:D12,D14:G24,D26:D30,D32:D34)] <> 66 Then
'Not all filled
Else
'All filled
End If
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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