Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can anyone explain this VBA code to me please

Status
Not open for further replies.

Husameddin

Technical User
Mar 4, 2003
17
GB
Hello everyone

Can anyone explain this VBA code to me please?

Function GiveColour(Years As Integer, Rank As Integer) As String
Select Case Years
Case Is>25
If Rank=12 Than
Answer=”Blue”
Else: answer=”Black”
End If
Case Is>15
If Rank<21 Than
Answer=”white”
Else:answer=”Purple”
End If
Case Else
Answer=”Brown”
End Select
GiveColour=answer
End Function

The question is

What colour corresponding to GiveColour (26, 21) ?
And how did you get the answer please.

Thank you in advance
 
Colour would be Black
1st look at Years
1st condition says if Years > 25
Years in this case = 26 therefore, we must follow this line
Next test is If Rank = 12. Well, rank = 21 in this case therefore doesn't = 12 therefore, according to the Else staement, the cour returned is Black
Basically, you have a select case which splits out a set of options. Then, within that option, there is an IF test , TRUE , FALSE staement which determines the output

Rgds
Geoff
&quot;Some cause happiness wherever they go; others whenever they go.&quot;
-Oscar Wilde
 
>What colour corresponding to GiveColour (26, 21) ?
Black is the answer that would be returned.

For the example you gave this portion of code is of interest to you:
Code:
Case Is>25
If Rank=12 Then
Answer=”Blue”
   Else: answer=”Black”
 End If
It basically checks if Years is greater than 25, if so then proceed to next statement. If Rank = 12 then return &quot;Blue&quot; as the answer string. Else return &quot;Black&quot; as the answer string.

So in the case of GiveColour (26, 21), Years is 26 so it's greater than 25 and Rank is 21 so it's not equal to 12 hence &quot;Black&quot; is the returned string.

If you want more explanation then let me know!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
thank you Xlbo

but what about the other If Rank which says
Case Is >15. 26 is more than 15
that what confused me...
so should I ignore the other part of the code?

thank you
 
If control has entered one of the Case statements then control is passed to the line after &quot;End Select&quot; i.e. GiveColour=answer. So because, in this specific case, the Case Is > 25 has already been entered it means that it will not enter the Case Is > 15 section. Only one of the Case sections can be entered into. Just to clarify, it is equivalent to:
Code:
if Years > 25 then
...
elseif Years > 15 then
...
end if

Hope this helps

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
To make it clearer (whilst keeping the same functionality), you could change
Case >15
to
Case >15 and <=25

Rgds
Geoff
&quot;Some cause happiness wherever they go; others whenever they go.&quot;
-Oscar Wilde
 
Thank you both for your help, I really appreciate it.

 
Hi
What happens is that the logical tests are carried out in the order they appear until one is true so 26 > 25 is true so the other case select tests are ignored.

If the code had been written in a different way ie Case Is>15 appeared before caseIs>25 you would get a different result. In fact the criteria caseis>25 would never be met.

Try putting a break point on the first line of the function (Select Case Years) then enter the function with diff values and step thru the code using F8 to see what happens. Then try swapping the position of >15 with >25 and do the same.

It's all good fun, honest!

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top