0

currently I figured out how to add multiple gradient stops with the code below. However, this way, I have to put RGB number in the code.

What I wanted to do is to get the color from another shape and put that color into the gradient stops instead of putting the RGB number.

I tried these lines but they don't work, any idea how to make it work? Thank you!

Function Get_Color(Index As Long) As Long
Get_Color = ActiveSheet.Shapes.Range(Array("Fill")).Fill.ForeColor
End Function

Sub gradients() 

Dim Point_Index As Long

 ActiveSheet.Shapes("Sauqre1").Select
 With ActiveSheet.Shapes("Sauqre1").Fill
 .ForeColor.RGB = RGB(0, 128, 128) 
 .OneColorGradient msoGradientHorizontal, 1, 1 
 .GradientStops.Insert RGB(255, 0, 0), 0.25 *This way work ok
 *But I want to get color from another shape, I tried these 3 lines, none of them work so far.
.GradientStops.Insert RGB = Get_Color(Point_Index) 
.GradientStops.Insert.RGB = Get_Color(Point_Index)
.GradientStops.Insert.Color.RGB = Get_Color(Point_Index)
 End With 
End Sub 

1 Answer 1

1
Function Get_Color(ShapeName As Variant) As Long
    Get_Color = ActiveSheet.Shapes(ShapeName).Fill.ForeColor
End Function

Sub gradients()
    Dim Point_Index As Variant ' may be name of shape or index of shape
    
    Point_Index = "OtherShape" ' or 2 if index of "OtherShape" is 2
    
    With ActiveSheet.Shapes("Sauqre1").Fill
       .ForeColor.RGB = RGB(0, 128, 128)
       .OneColorGradient msoGradientHorizontal, 1, 1
       .GradientStops.Insert Get_Color(Point_Index), 0.25
    End With
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

OMG! YESS, this magical, thank you so much.
May I ask one more question please if I wanted to add .TintAndShade below this line, is that doable? For example, .GradientStops.Insert Get_Color(Point_Index), 0 .GradientStops.TintAndShade = -0.4
Yes, e.g. .ForeColor.TintAndShade = 0.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.