run macro on condition

isildad

Active Member
Joined
Sep 22, 2004
Messages
325
In range A1:A5 if any cell value >0 then ran macro?
how to write this in VBA?
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
OK like that (more complicate):
Code:
If Application.CountA(Range("A1:A5")) = 0 Then
    Exit Sub
    Else
Call macro
 
Upvote 0
How are the values in that range changing? Via formula or manually? For the former you can use your existing logic in a Calculate event, for the latter you can use a Change event.


HTH,
 
Upvote 0
Manually
too much code in present changeevent prefer to use as is above but need to have additional condition ex:
Code:
If Application.CountA(Range("A1:A5")) = 0 Then
    Exit Sub
'here add 2nd condition
  ' OR If ("B1").value = "cancel"
'Exit Sub

    Else
Call macro

thkx
 
Upvote 0
correct code but not good for this situation (my bad) because if B1=cancell then code to delete sheet

maybe with "case" like this?
(do not know how to right case code)

Code:
case Application.CountA(Range("A1:A5")) = 0 
case exit sub

case Range("B1").Value = "Cancel"
case exit sub 
delete sheet

case Application.CountA(Range("A1:A5")) = >0 
case call macro
 
Upvote 0
I'm not sure what you're trying to do. I'd probably leave the = 0 test out, since all it looks like you're doing is Exiting the sub anyway, and focus on the other 2 conditions.

Code:
If Application.CountA(Range("A1:A5")) = >0 Then 
  Call Macro
If Range("B1").Value = "Cancel" Then 
  Call DeleteSheet
End if
End If
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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