ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If then several times (https://www.excelbanter.com/excel-programming/339065-if-then-several-times.html)

John

If then several times
 
I seem to be having a problem running a macro that uses several if then
statements. Here is an example... why won't the second third, fourth if
statements run?

If Activecell.value = 1 then
DO whatever
end if
If activecell.value =2 then
do whatever
end if
Ifactivecell.value = 3 then
do whatever
end if
end sub

Tushar Mehta

If then several times
 
What do you mean by "why won't...if statements run?" Do you mean they
are not evaluated? Or that the 'do whatever' part is not executed?

What is the value of the active cell? What does the first 'DO
whatever' actually contain?

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I seem to be having a problem running a macro that uses several if then
statements. Here is an example... why won't the second third, fourth if
statements run?

If Activecell.value = 1 then
DO whatever
end if
If activecell.value =2 then
do whatever
end if
Ifactivecell.value = 3 then
do whatever
end if
end sub


John

If then several times
 
The "activecell.value" had text and only worked when I matched the case of
the letters in the text..., I thought that didn't matter, but apparently it
does.

Sorry and thanks again...

"Tushar Mehta" wrote:

What do you mean by "why won't...if statements run?" Do you mean they
are not evaluated? Or that the 'do whatever' part is not executed?

What is the value of the active cell? What does the first 'DO
whatever' actually contain?

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I seem to be having a problem running a macro that uses several if then
statements. Here is an example... why won't the second third, fourth if
statements run?

If Activecell.value = 1 then
DO whatever
end if
If activecell.value =2 then
do whatever
end if
Ifactivecell.value = 3 then
do whatever
end if
end sub



leaftye - ExcelForums.com

If then several times
 
If your code is embedded in a procedure or function, and the "d
whatever" has an "exit sub" line in it, then it won't proces
anything else that follows it in that section of code

You're also better off using a Case structure, or throwin
activecell.value into a variable so you don't repeatedly retrieve th
same information over and over and over..


John

If then several times
 
there is no "exit sub" in the code... however, your suggestion about seeting
variables is a good one.

Thanks

"leaftye - ExcelForums.com" wrote:

If your code is embedded in a procedure or function, and the "do
whatever" has an "exit sub" line in it, then it won't process
anything else that follows it in that section of code.

You're also better off using a Case structure, or throwing
activecell.value into a variable so you don't repeatedly retrieve the
same information over and over and over...



John

If then several times
 
What is "case structure"?

"leaftye - ExcelForums.com" wrote:

If your code is embedded in a procedure or function, and the "do
whatever" has an "exit sub" line in it, then it won't process
anything else that follows it in that section of code.

You're also better off using a Case structure, or throwing
activecell.value into a variable so you don't repeatedly retrieve the
same information over and over and over...



KobusD

If then several times
 
I think he means:

Select Case "ActiveValue"
Case 1
'code goes here
Case 2
'code goes here
Case 3
'code goes here
Case Else ' Incorrect value.
'code goes here
End Select



All times are GMT +1. The time now is 10:58 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com