gpt4 book ai didi

Excel slider 控件 : How could I limit the sum of all sliders to, 比如说,100?

转载 作者:行者123 更新时间:2023-12-02 17:10:39 25 4
gpt4 key购买 nike

查看图片以确保清晰度。

Slider

我有 5 个变量(A、B、C、D 和 E),每个变量的范围为 0-100。我需要所有这些变量的总和始终为 100,不能多也不能少。然而,按照目前的设置方式,如果我将变量 A 从 21 更改为 51,那么总数将变为 130。

如何设置,以便如果我更改一个变量,其他变量可以自动补偿该增加或减少,使总数始终 100?

最佳答案

使用 slider 更改事件,这样当一个 slider 更改值时,其他 slider 就会缩放,使值总和为 100

示例代码,使用 3 个 slider - 您可以对其进行缩放以允许使用任意数量的 slider

Private UpdateSlider As Boolean

Private Sub ScaleSliders(slA As Double, ByRef slB As Double, ByRef slC As Double)
Dim ScaleFactor As Double
If (slB + slC) = 0 Then
ScaleFactor = (100# - slA)
slB = ScaleFactor / 2
slC = ScaleFactor / 2

Else
ScaleFactor = (100# - slA) / (slB + slC)

slB = slB * ScaleFactor
slC = slC * ScaleFactor
End If
End Sub


Private Sub ScrollBar1_Change()
Dim slB As Double, slC As Double
' UpdateSlider = False
If Not UpdateSlider Then
slB = ScrollBar2.Value
slC = ScrollBar3.Value
ScaleSliders ScrollBar1.Value, slB, slC
UpdateSlider = True
ScrollBar2.Value = slB
ScrollBar3.Value = slC
UpdateSlider = False
End If
End Sub

Private Sub ScrollBar2_Change()
Dim slB As Double, slC As Double
If Not UpdateSlider Then
slB = ScrollBar1.Value
slC = ScrollBar3.Value
ScaleSliders ScrollBar2.Value, slB, slC
UpdateSlider = True
ScrollBar1.Value = slB
ScrollBar3.Value = slC
UpdateSlider = False
End If
End Sub

Private Sub ScrollBar3_Change()
Dim slB As Double, slC As Double
If Not UpdateSlider Then
slB = ScrollBar1.Value
slC = ScrollBar2.Value
ScaleSliders ScrollBar1.Value, slB, slC
UpdateSlider = True
ScrollBar1.Value = slB
ScrollBar2.Value = slC
UpdateSlider = False
End If
End Sub

请注意, slider 数据类型为整数,因此您可能需要允许舍入而不是总和为 100

关于Excel slider 控件 : How could I limit the sum of all sliders to, 比如说,100?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6355205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com