donali
donali

Reputation: 11

Change colour of shapes by randomly selecting from a specified list of RGB colours

I would like to change the colour of the shapes in a PowerPoint presentation by selecting randomly from a list of predefined colours. This is the code that I am using, but it of course only allows a single colour. I think I need to use an array, but how do I do so?

Sub ChangeShapeColor()

    Dim oSh As Shape
    Dim oSl As Slide

    ' Look at each slide in the current presentation:
    For Each oSl In ActivePresentation.Slides

        ' Look at each shape on each slide:
        For Each oSh In oSl.Shapes

            ' IF the shape's .Fill.ForeColor.RGB = turqoise color:
           If oSh.Fill.ForeColor.RGB = RGB(68, 114, 196) Then

                ' Change it to corporate dark grey:
            oSh.Fill.ForeColor.RGB = RGB(195, 14, 96)

           End If

        Next oSh

    Next oSl

End Sub

Upvotes: 1

Views: 581

Answers (1)

Christofer Weber
Christofer Weber

Reputation: 1474

Most basic approach;
Dim a variable as variant and set a bunch of colors to it:

Dim arr As Variant
arr = Array(RGB(190, 0, 0), RGB(255, 0, 0), RGB(255, 130, 0), RGB(0, 176, 80), RGB(240, 240, 0), RGB(250, 10, 10))

And then pick a random number from the array each loop, instead of a specific one:

oSh.Fill.ForeColor.RGB = arr(Int(Rnd * (UBound(arr))))

Upvotes: 3

Related Questions