【问题标题】:Why Shape Format changes for other Shapes while selecting Single为什么选择单个时其他形状的形状格式会发生变化
【发布时间】:2021-12-21 13:14:06
【问题描述】:

我正在尝试创建一个函数,该函数保持所有形状的颜色和形状格式不变,但正在选择的单个形状除外。之后,如果我选择其他,则应使用与之前相同的颜色和格式进行转换。

但我的函数会更改所有形状的颜色和格式。

我有这种格式。

当我运行该代码时,它会改变所有人的颜色。

我不知道如何实现这一点,非常感谢您的帮助。

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

【问题讨论】:

    标签: excel vba shapes


    【解决方案1】:

    使用 Application.Caller 您可以捕捉被点击的形状,并让您最小化您的代码。在此示例中,我重命名了选择窗格中的形状,如图所示。

    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
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 1970-01-01
      • 2018-04-07
      • 2016-03-12
      • 1970-01-01
      • 2020-04-15
      • 2021-12-03
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多