Case compile error - case without select case

tweek

Board Regular
Joined
Jun 16, 2006
Messages
104
Hi.

I´m trying out CASE for the first time in VB. I´m starting out with a simple cell format but it gives me an error on case 2, please look at the code below. Can anybody give me a reason why and maybe some pointers?

Thank you.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Sub AAA()

Number = 4
Select Case Number
Case 1
Cells(1, 10).Select
With Selection.Interior
.Color = 255
Case 2
Cells(1, 11).Select
With Selection.Interior
.Color = 255
Case 3
Cells(1, 12).Select
With Selection.Interior
.Color = 255
Case 4
Cells(1, 13).Select f
With Selection.Interior
.Color = 255
Case 5
Cells(1, 14).Select
With Selection.Interior
.Color = 255
Case Else
Cells(2, 14).Select
With Selection.Interior
.Color = 255
End Select

End Sub
 
Upvote 0
Keep it simple - do not complicate matters.

Code:
Sub AAA()
    Number = 4
    Select Case Number
        Case 1 To 5: Cells(1, Number + 9).Interior.Color = 255
        Case Else: Cells(2, 14).Interior.Color = 255
    End Select
End Sub

Please use
Code:
 tags when pasting code to the Board here. Thanks.
 
Upvote 0
Thank you! that works great.

But do you or anybody know why my code didn´t work?
 
Upvote 0
You did not close the With statements by using "End With".

Indenting code would allow you to catch that error without effort.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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