Simple question regarding the use of the IF-statement in VBA in Excel

kristiankoehn

New Member
Joined
Jun 14, 2012
Messages
4
I am not familiar enough with VB, but need to program the following:
In the excel spreadsheet, I have a variable (AGE) wich is a number. Depending on the size of this variable, I want to make a VB-macro which executes different other macros depending on the value of AGE.

In excel (assuming that "Macro1" would execute a macro - which it will not, I know...!), I would state this like IF(AGE<6;"Macro1";IF(AGE>6;"Macro2";"")) and so forth.

How do I express this in VB? I guess it is pretty straightforward...?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I forgot to mention that the programming will also need to take account of the need to define intervals. By that I mean that I should be able to select the function on the basis of a value greater than AND less than a value. In excel terms: AND(age>=6;age<8).
 
Upvote 0
Hi kristiankoehn,

Try something like this:

Code:
Sub FAge(): Dim Age As Integer
If Age >= 6 And Age < 8 Then
Macro1
ElseIf Age > 8 Then
Macro2
End If
End Sub
 
Upvote 0
Hm, I am probably not very clever here, but I cannot get the macros executed. I typed in this code (Slightly altered, in norwegian...):

Sub FAlder(): Dim Alder As Integer
If Alder >= 6 And Alder < 8 Then
Vis_6til8
ElseIf Alder > 8 Then
Vis_8til16
ElseIf Alder >= 8 And Alder < 16 Then
Vis_8til16
End If
End Sub
 
Upvote 0
Sorry for jumping in here... But is it just me or did you miss "Call"ing the macros....???? Try that... I assume Vis_6til8 is the name of your macro.... (It might be more complex than that but from a quick glance it looks like if you add "call" before your macro's it should execute your macros.....

Sub FAlder(): Dim Alder As Integer
If Alder >= 6 And Alder < 8 Then
Call Vis_6til8
ElseIf Alder > 8 Then
Call Vis_8til16
ElseIf Alder >= 8 And Alder < 16 Then
Call Vis_8til16
End If
End Sub
 
Upvote 0
The order of you if statements do not appear to make sense.

you have the option of between 6 & 8 which is okay, but then greater than 8 followed between 8 & 16. I t won't go to the last option as it has already used the greater than 8 option.

If you want between 6 & 8, then 8 & 16, then greater than 16 try:


Code:
Sub FAlder()


Dim Alder As Integer




    If Alder >= 6 And Alder < 8 Then
        Vis_6til8
    ElseIf Alder >= 8 And Alder < 16 Then
        Vis_8til16
    ElseIf Alder >= 16 Then
        Vis_gt16
    End If
End Sub

I have changed the name of the greater than macro as the name you provided was repeated. (No need for call function although some use it to make code easier to read).

Also I am assuming you assign a value to alder else where.
 
Upvote 0

Forum statistics

Threads
1,206,945
Messages
6,075,782
Members
446,156
Latest member
Aaro23

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