【问题标题】:Changing arc length in excel based on a cell value根据单元格值更改excel中的弧长
【发布时间】:2018-08-11 06:29:54
【问题描述】:

我想根据单元格值动态更改 Excel 中的弧长。 例如,如果单元格值 = 100%,则拱门应成为一个完整的圆。如果值 = 0,它应该消失。 我发现下面的代码改变了形状的大小,但我不知道如何修改它来改变长度。

示例:

非常感谢您的帮助。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xAddress As String
    On Error Resume Next
    If Target.CountLarge = 1 Then
        xAddress = Target.Address(0, 0)
        If xAddress = "CT15" Then
            Call SizeCircle("Block Arc 63", Val(Target.Value))
        End If
    End If
End Sub

Sub SizeCircle(Name As String, Diameter)
    Dim xCenterX As Single
    Dim xCenterY As Single
    Dim xCircle As Shape
    Dim xDiameter As Single
    On Error GoTo ExitSub
    xDiameter = Diameter
    If xDiameter > 10 Then xDiameter = 10
    If xDiameter < 1 Then xDiameter = 1
    Set xCircle = ActiveSheet.Shapes(Name)
    With xCircle
        xCenterX = .Left + (.Width / 2)
        xCenterY = .Top + (.Height / 2)
        .Width = Application.CentimetersToPoints(xDiameter)
        .Height = Application.CentimetersToPoints(xDiameter)
        .Left = xCenterX - (.Width / 2)
        .Top = xCenterY - (.Height / 2)
    End With
ExitSub:
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您可以使用Shapes.Adjustments属性来调整块弧的“长度”。

    Procedure AdjustArc 会将指定的形状设置为指定的“% complete”。

    Procedure Demo 将“动画化”您的形状进度。确保在运行演示之前根据需要更改工作表名称和形状名称。过程Pause 只是Demo 的装饰。

    Sub AdjustArc(arcShape As Shape, percent As Single)
    '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
    
    Sub demo() 'Run this one for demonstration
        Dim ws As Worksheet, sh As Shape, x As Single
        Set ws = ThisWorkbook.Sheets("Sheet1")
        Set sh = ws.Shapes("Block Arc 1")
        For x = 0 To 1 Step 0.005
            AdjustArc sh, x
            Pause 0.01
        Next x
    End Sub
    
    Sub Pause(seconds As Single) 'just for the demo
    'pause for specified number of seconds
        Dim startTime As Single: startTime = Timer
        Do: DoEvents: Loop Until Timer >= startTime + seconds
    End Sub
    

    短版:

    改变形状的线是:

    ActiveSheet.Shapes("YourShapeName").Adjustments.Item(1) = x
    

    ...其中x 是一个值&gt; 0 and &lt; 360


    编辑:适应你的代码

    当前,当工作表的单元格 CT15 发生更改时,您的示例代码会调用 SizeCircle

    你可以替换这一行:

    Call SizeCircle("Block Arc 63", Val(Target.Value))
    

    ...用这个:

    AdjustArc ThisWorkbook.Sheets("Sheet1").Shapes("Block Arc 63"),Val(Target.Value) 
    

    只需将Sheet1 替换为具有该形状的工作表的名称即可。

    这是假设百分比存储CT15 中的实际百分比(0 到 1)...如何格式化并不重要。

    您的代码和我的SizeCircle 过程应该位于工作表模块(因为它有一个on_change 事件),您可以通过右键单击工作表的选项卡并单击View Code 打开该模块。


    更多信息:

    【讨论】:

    • 谢谢一百万!看起来很完美,但我想知道包含 %complete 的单元格的链接在哪里?对不起,我只是一个初学者:(
    • 哦,我有点过于复杂了...... :) 我会在我的答案中添加一个注释。
    • 非常感谢您的帮助,但您能否简化代码 :) 不需要动画,因为它在我的文件上无法正常工作(即使 %=0 它是动画的)
    • 好的,我添加了进一步的解释。如果您卡在任何地方,您可以发布一个新问题,其中包含您卡住的特定代码部分,并解释您正在尝试做什么以及您尝试了什么。祝你好运! (也只是提醒一下,如果答案有助于解决您的问题,请不要忘记✓Accept。)
    • @Ahmed - 只需忽略 demopause 过程。真的只有 一行 改变了形状:ActiveSheet.Shapes("Block Arc 1").Adjustments.Item(1)=(1-percent) * 359.9
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2019-10-25
    • 2020-11-28
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    相关资源
    最近更新 更多