Select Case on Iterator Variable Not Working As Expected

ExcelChemist

New Member
Joined
Dec 9, 2011
Messages
16
I have had this problem in multiple different macros so I will just provide an example of relevant code:
Code:
For J = 0 To 3

    Select Case J
    Case J = 0
    LikeVar = "a*spk1*E0"
    LikeVar2 = "a*spk1*E7"
    
    End Select
Next J

For some reason it skips over the likevar variable assignments even though J = 0 on the first time through the loop. I have tried changing the For line to J = 1 to 3, and the case to J = 1, but it still skips to the end select. Is it not possible to use a select case on an iterator variable? Thanks in advance for any input.
 
It should read

Code:
Select Case J
Case 0
...
instead of
Code:
Select Case J
Case J = 0
...

You already delcared the case to examine as J in the line above.
 
Upvote 0
Code:
For J = 0 To 3

    Select Case J
    Case 0
    LikeVar = "a*spk1*E0"
    LikeVar2 = "a*spk1*E7"
    
    End Select
Next J

otherwise you are comparing J to (J = 0) which is comparing 0 to True.
 
Upvote 0
THis line:

Case J = 0

is actually the same as

Case True

when j=0, thus you don't get a match.

You therefore simply need:

Case 0
 
Upvote 0

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