Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

excel - VBA getting multiple gradient Color from another shape color

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 
question from:https://stackoverflow.com/questions/65921389/vba-getting-multiple-gradient-color-from-another-shape-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...