Run a part of macro when a condition is met

newbie_ex

New Member
Joined
Dec 15, 2013
Messages
9
Hi,
I m really new to excel macros and dont know the jargon, so please explain in a simple way.
I am trying to compile a macro in the middle of which i have assumed a variable "counter" where counter = value in I3 (which may be any whole number)
I think that i have properly done this step as:
dim counter as long
Range("I3").select
counter = activecell.value

Now i have three different steps to be performed on the basis of the value of counter
first code is to be executed only if value of counter is 0 or 1
second code is to be executed when value of counter is 2
third code is to be executed when value of counter is 3 and above

Kindly help
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi newbie,

Just use if else to follow your steps
Code:
if (counter= 0 ) or (counter =1) then 

write your code here

elseif counter = 2 

write here your code

elseif counter =3 

write your code here

end if
 
Upvote 0
Hi,

I am no expert myself, but this one is not too difficult.

Like writing a letter, there are many ways to write code that does the same thing, although the exact contents of the text differs. I never realized that before, but I think it is fun.

A book you definately want to get your hands on is "VBA Excel for Dummies".

I will post some code here to figure out how to do that on this forum (I promised to do this with some of the code I hammered together myself for a project I worked on).

Code:
Option Explicit

Sub DoSomething()

Dim Counter as Long

Counter = Range("I3").value

If Counter >=0 And Counter < 2 Then
     'example code to see it work:
     MsgBox ("0 >= Counter <2")
ElseIf Counter = 2 Then
     MsgBox ("Counter = 2")
ElseIf Counter > 2 Then
     MsgBox ("Counter >= 3")
End If

End Sub 'DoSomething()

(Oke, posting code this way was easier than I thought).

Have fun with VBA. You can let it do amazing things! Just don't give up trying.

Cheers,

Ro
 
Upvote 0
Here is another option for you to consider:

Code:
Sub Test()

Dim Counter As Long
Counter = Range("I3").Value

Select Case Counter
    Case Is >= 3
    'some code for greater than 3
    Case Is >= 2
    'some code for between 2 and 3
    Case Is >= 0
    'some code for between 0 and 2
End Select

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,918
Members
449,195
Latest member
Stevenciu

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