user1775842
user1775842

Reputation: 53

Using Select Case in excel vba

Just curious but is there any way to reduce this to smaller number of lines of code?

For k = 1 To 12
    Select Case k
        Case 1
            col = 9
        Case 2
            col = 10
        Case 3
            col = 11
        Case 4
            col = 12
        Case 5
            col = 13
        Case 6
            col = 14
        Case 7
            col = 15
        Case 8
            col = 16
        Case 9
            col = 17
        Case 10
            col = 18
        Case 11
            col = 19
        Case 12
            col = 20
    End Select
Next

Thanks!

Upvotes: 3

Views: 3323

Answers (1)

Daniel
Daniel

Reputation: 13142

How about:

For k = 1 to 12
   col = k + 8
Next

Or what you were probably looking for:

For k = 1 To 12
    Select Case k
        Case 1 To 12
            col = k + 8
    End Select
Next

Here's the relevant MSDN, you can scroll down for examples.

Upvotes: 3

Related Questions