VBA stop procedure if A1=0

cazdealer

Board Regular
Joined
Mar 12, 2011
Messages
96
Hi,
I created a long macro but I don't want it to run if the value of cell A1=0

How could I write something at the beginning of my code that will give me a message box saying A1=0 (if it the case) and stop running... and if A doesnt equal 0, I just want to macro to continue.

thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try:

Code:
Sub MySub()
If Sheets("SheetName").Range("A1").Value = 0 Then
Exit Sub
Else
Rest of Code
End Sub

Hank
 
Upvote 0
Hi Hank,
I tried this below and I get this message:

Compile error:
Block If without End if

do you know what is wrong?



Code:
Sub test()

If Sheets("data").Range("A1").Value = 0 Then
Exit Sub

Else
    Range("A1:B14").Select
    With Selection.Font
        .Color = -16776961
        .TintAndShade = 0
        
    End With
    
    Range("F4").Select
    ActiveCell.FormulaR1C1 = "test"
    Range("E4").Select

End Sub
 
Upvote 0
Stupid me!

Sorry, just zoned out. Use this:

Code:
Sub test()

If Sheets("data").Range("A1").Value = 0 Then
Exit Sub

Else
    Range("A1:B14").Select
    With Selection.Font
        .Color = -16776961
        .TintAndShade = 0
        
    End With
End If
    
    Range("F4").Select
    ActiveCell.FormulaR1C1 = "test"
    Range("E4").Select

End Sub
 
Upvote 0
Just test for the value in A1 directly at the beginning of your code and pop your MessageBox if it is, then follow that with and "Exit Sub" statement so that your existing code doesn't run....
Code:
If Range("A1").Value = 0 Then
  MsgBox "Cell A1 contains 0"
  Exit Sub
End If
'
'  Your existing code goes here
'
Here I have assumed the code is running from the active worksheet; if that will not always be the case, then prepend the Range call with the worksheet its on (assumed here to be Sheet1)...
Code:
If Worksheets("Sheet1").Range("A1").Value = 0 Then
  MsgBox "Cell A1 contains 0"
  Exit Sub
End If
'
'  Your existing code goes here
'
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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