根据单元格值动态更改弧长(Excel)

时间:2018-08-11 09:15:34

标签: excel vba excel-vba

我正在尝试根据特定单元格的值在Excel中调整弧长。

例如,如果%= 100,则圆弧应成为一个圆。

我使用的代码无法正常工作。当我更改单元格(A1)中的%值时,(块弧1)的弧长不会改变。有人可以帮忙吗:)?

Sub AdjustArc(arcShape As Shape, percent As Single)
    Dim xAddress As String
    On Error Resume Next
    If Target.CountLarge = 1 Then
        xAddress = Target.Address(0, 0)
        If xAddress = "A1" Then
            AdjustArc ThisWorkbook.Sheets("Sheet1").Shapes("Block Arc 1"), Val(Target.Value)
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我看到了您的previous post

您应该需要以下内容

  1. Worksheet_Change事件处理程序

    将以下代码放置在弧形所驻留的工作表的代码窗格中(并确保该形状以“ Block Arc 1”命名)

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.CountLarge = 1 Then
            If Target.Address(0, 0) = "A1" Then AdjustArc Shapes("Block Arc 1"), Target.Value
        End If
    End Sub
    
  2. AdjustArc

    将AdjustArc()子项(@ashleedawg的礼貌)放置在所需的任何模块中

    Sub AdjustArc(arcShape As Shape, percent As Single)
    ' ashleedawg 's courtesy in https://stackoverflow.com/questions/51797109/changing-arc-length-in-excel-based-on-a-cell-value
    'adjust the circumference of the arc or hides if 0%.
    'Supply the percent as a fraction between 0 and 1. (50% = 0.5)
    
        With arcShape
            If percent <= 0 Then 'hide shape
                .Visible = False
                Exit Sub
            End If
    
            If percent > 1 Then percent = 1 'over 100%, make it 100%
            .Visible = True
    
            '0 = Full Circle, 359.9 = sliver, 360 = Full Circle
            .Adjustments.Item(1) = (1 - percent) * 359.9
        End With
    End Sub