using end if inside an if loop

smakatura

Board Regular
Joined
May 27, 2011
Messages
141
my code looks like this so far


legend for coding/question below
  • newvariable is the variable name for an inputted value
  • ...A actions -the commands if the value is A
  • ...X actions - commands if the value is not A or B

HTML:
if newvariable = "A" then
     ....A Actions
else 
     .....X Actions
End if

I want to add a B value. if the value is B, I want it to do nothing - end the loop and move on with the macro...so something like

HTML:
if newvariable = "A" then
    ....A Actions
else if newvariable = "B" then
    endif
else 
    .....X Actions
End if

I think the first endif is probaby the wrong way to do it.

Essentally I can have 6 values- A, B, and X (where is X anything but A or B) . If A, do the A actions. If B, I want to exit the loop. If anything but A or B do the X actions.

what is the proper coding?
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
will "exit for" work within the if-endif statement?

I am not sure if something like "resume next" would be the right action.
 
Upvote 0
You could try something like this.
Not sure how many X actions you have. But the loop ends when newvariable="B"
note "the value of # is the number you have given to your subroutine"

Sub Main#()

do until newvariable ="B"
if newvariable="A"
then
..."A" actions
else
...."X" actions
endif
Loop

endsub
 
Upvote 0
bc I could not find the right wording for ending the loop with in the if statemetn, I went with the code below, it worked great


HTML:
if newvalue <> "B" then   
  if newvariable = "A" then
     ....A Actions
  else 
     .....X Actions
  End if
End if
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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