Home > database >  Why Shape Format changes for other Shapes while selecting Single
Why Shape Format changes for other Shapes while selecting Single

Time:12-22

I am trying to create a function which keeps the Color and Shape format for all shapes as it is except for single which is being selected. after that if i select other then it should be converted with same Color and format it has before.

But my function chnages the shape color and format for all.

I have this format.

enter image description here

When i run that code it changes color for all.

enter image description here

I do not know how to achieve this your help will be much appreciated.

Sub Shape1()
Sheet1.Shapes("Group 16").ShapeStyle = msoShapeStylePreset41
Sheet1.Shapes("Freeform 17").ShapeStyle = msoShapeStylePreset27
Sheet1.Range("B5").Value = "January"
End Sub


Sub Shape2()
Sheet1.Shapes("Group 16").ShapeStyle = msoShapeStylePreset41
Sheet1.Shapes("Freeform 18").ShapeStyle = msoShapeStylePreset27
Sheet1.Range("B5").Value = "February"
End Sub

Sub Shape2()
Sheet1.Shapes("Group 16").ShapeStyle = msoShapeStylePreset41
Sheet1.Shapes("Freeform 19").ShapeStyle = msoShapeStylePreset27
Sheet1.Range("B5").Value = "March"
End Sub

CodePudding user response:

Using Application.Caller you catch the shape that was clicked and let's you minimize your code. In this example I renamed the shapes in the Selection Pane as shown in the picture.

enter image description here

Sub Months_ProcessShape()
    Dim callingShape As Variant
    Dim rCell As Range: Set rCell = Sheet1.Range("K5")      ' Month cell
    
    ' Calling shape
    callingShape = Application.Caller
    
    ' Set color
    Sheet1.Shapes("shGrp_Months").ShapeStyle = msoShapeStylePreset41
    Sheet1.Shapes(callingShape).ShapeStyle = msoShapeStylePreset27
    
    ' Write month in cell
    Select Case callingShape
        
        Case "shp_Jan"
            rCell.Value = "January"
            
        Case "shp_Feb"
            rCell.Value = "February"
            
        Case "shp_Mar"
            rCell.Value = "March"
            
        Case "shp_Apr"
            rCell.Value = "April"
            
        Case "shp_May"
            rCell.Value = "May"
    
    End Select
End Sub
  • Related