Macro validation before running

Cuzzaa

Board Regular
Joined
Apr 30, 2019
Messages
86
Hi guys

Hoping a genius can help me with a quick check before running through a macro...

My vba below works perfectly, however can someone please help add some validation before running my macro that does the following check:

Cell C9 must contain either the text "RP2000" or "RP5000"
Cell C11 must contain a value larger than 0
C13 must contain a value larger than 0
C15 must contain a value larger than 0

If all the above criteria is NOT met then when clicking the macro button a Msgbox is instead presented with a warning telling the user to enter the information required for the Cell's that have not been entered with an OK button which then ends the macro without running further?

Otherwise, if all the above criteria has been met then the macro will proceed to running.

Please see my current vba below:

Code:
Sub colour()Range("F9:I14").Interior.Color = RGB(242, 242, 242)
Range("F9:I14").Font.Color = RGB(0, 0, 0)


Range("F16:I16").Interior.Color = RGB(149, 0, 46)
Range("F16:I16").Font.Color = RGB(255, 255, 255)




Range("F19:I43").Interior.Color = RGB(242, 242, 242)
Range("F19:I43").Font.Color = RGB(0, 0, 0)
Range("F4").Select
    ActiveCell.FormulaR1C1 = "Please find approximate BOM below:"
    Range("A1").Select
    
    
    
    
    
    Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("I22:I45")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
         
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True
    
       MsgBox "The BOM has been generated!", vbInformation
    
    
End Sub

Thank you so much in advance!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Re: Help please - macro validation before running

Code:
Sub Data_Validator()
    If Range("C9") = "RP2000" Or Range("C9") = "RP5000" Then
        If Range("C11") > 0 And Range("C13") > 0 And Range("C15") > 0 Then
            'Your Code Here
        Else
             msgbox "Code Halted because BLAH BLAH" 
        End If
    End If
End Sub
 
Last edited:
Upvote 0
Re: Help please - macro validation before running

Another option.
Add this to the start of your macro
Code:
If Range("C9") <> "RP2000" And Range("C9") <> "RP5000" _
   Or (Range("C11") <= 0 Or Range("C13") <= 0 Or Range("C15") <= 0) Then
      MsgBox "Please complete all cells"
      Exit Sub
End If
 
Upvote 0
Re: Help please - macro validation before running

Another option.
Add this to the start of your macro
Code:
If Range("C9") <> "RP2000" And Range("C9") <> "RP5000" _
   Or (Range("C11") <= 0 Or Range("C13") <= 0 Or Range("C15") <= 0) Then
      MsgBox "Please complete all cells"
      Exit Sub
End If

Thanks Steve and Fluff!! That's perfect!!!
 
Upvote 0
Re: Help please - macro validation before running

Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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