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
?
CodePudding user response:
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
CodePudding user response:
You can use Select Case
For Each Shape in Shapes
Select Case Shape.Name
Case "Oval 23", "Oval 39", "Oval 61", "Oval 72", "Oval 98"
'Do something
Case Else
'Do nothing
End Select
Next Shape
There is some merit to use this method, i.e. you can easily add or change a procedure of a particular shape
.
The caveat is it would loop through all Shapes
, so may be a little slower than using the array() method as suggested by @Raymond, if there are more than 5 shapes in the slide.