Search code examples
vbapowerpoint

Select Case - Loop patternless series


With nGameSlide
.Shapes("Oval 23").Fill.ForeColor.RGB = RGB(255, 192, 0)
.Shapes("Oval 39").Fill.ForeColor.RGB = RGB(255, 192, 0)
.Shapes("Oval 61").Fill.ForeColor.RGB = RGB(255, 192, 0)
.Shapes("Oval 72").Fill.ForeColor.RGB = RGB(255, 192, 0)
.Shapes("Oval 98").Fill.ForeColor.RGB = RGB(255, 192, 0)
End With

We could use For i = 1 to 5 Step 1 if the shapes were in a sequence, however in cases like the above, is it possible to use Select Case? How would be go about simplifying the code? Would it be something along the lines of Select Case 23, 39, 61, 72, 98?


Solution

  • Your proposal for using Select Case is entirely incorrect.

    Building on the suggestion by @Raymond Wu its better to use a loop, however, I'd use a for each loop as its simpler syntax.

    Dim myNum as variant
    For each myNum in Array(23, 39, 61, 72, 98)
    
        nGameSlide.Shapes("Oval " & CStr(myNum)).Fill.ForeColor.RGB = RGB(255, 192, 0)
    
    Next
    

    A better approach would be to encapsulate the for each loop in a sub and pass in the numbers to use as a parameter.

    Public Sub PaintSlides(byref ipArray as variant)
    
        Dim myNum as variant
        For each myNum in ipArray
    
            nGameSlide.Shapes("Oval " & CStr(myNum)).Fill.ForeColor.RGB = RGB(255, 192, 0)
    
        Next
    
    end sub