【问题标题】:Excel VBA loop changes numbers in Option button nameExcel VBA 循环更改选项按钮名称中的数字
【发布时间】:2018-02-23 21:34:56
【问题描述】:

我正在编写一个带有宏用户表单的 Excel 表格,以替换我们工作场所的纸质系统。它是一个有两个框架的简单形式;一个包含 3 个选项按钮,另一个包含一个文本框,然后还有 3 个选项。我写的代码是这样的..

Dim Option1A As String
If Frame1.OptionButton1Low Then
Option1A = "Low"
ElseIf Frame1.OptionButton1Mid Then
Option1A = "Medium"
ElseIf Frame1.OptionButton1High Then
Option1A = "High"
End If

Dim Risk1 As String
Risk1 = TextBox1.Value
If Option1A = "Medium" And Risk1 = "" Then
MsgBox ("Please enter a description of your risk mitigation")
ElseIf Option1A = "High" And Risk1 = "" Then
MsgBox ("Please enter a description of your risk mitigation")
End If

Dim Option1B As String
If FrameF1.OptionButton1FLow Then
Option1B = "Low"
ElseIf FrameF1.OptionButton1FMid Then
Option1B = "Medium"
ElseIf FrameF1.OptionButton1FHigh Then
Option1B = "High"
End If

ActiveCell.Offset(g, 15).Value = Option1A
ActiveCell.Offset(g, 16).Value = Risk1
ActiveCell.Offset(g, 17).Value = Option1B

简单吧?这正是我想要的,问题是我在这段代码中有 1 对框架,用户窗体上还有 17 对。所有的文本框、框架、选项按钮和字符串都是按顺序命名的。所以现在我可以再复制粘贴 17 次,得到我想要的结果。

我想知道是否有办法编写一个 Do While 循环来更改数字,因此 Option1A 变为 Option2A、Option3A 和 OptionButton1FLow 变为 OptionButton2FLow 等。基本上将代码中的所有数字 1 更改为 x 值。

我在研究中没有找到任何建议,并且开始认为这是不可能的,

非常感谢任何见解,

提前干杯,

肖恩

【问题讨论】:

  • 对不起,糟糕的标题,我正在尝试任何单词组合来搜索!
  • 您的意思是您总共有 18 对或框架(即 36 个),其中每个包含 3 个选项按钮? g 的值是多少?我的意思是,我猜你想插入 17 行数据,因为你有 17 对,对吧?
  • 为什么不把这段代码包装到过程中,并用适当的控件作为参数调用它 18 次?
  • Foxfire - 总共 18 帧没有,是的,g 的值用于稍后将其插入工作簿的代码
  • 五。 Kisel - 这可能比我的技能水平略高,所以我现在开始阅读,谢谢你的提示!

标签: vba excel


【解决方案1】:

使用UserForm 对象的Controls 属性来按名称寻址其所有成员(如框架、文本框等)

另外你可以使用Switch()函数来缩短选项按钮的值检查和相应的值被采用

Dim iFrame As Long
Dim OptionA As String, Risk As String, OptionB As String 'this variables are just loop-scoped, hence no need to name them loop-wise. so let's name them without any numeric index

With Me
    For iFrame = 1 To 18
        OptionA = Switch(.Controls("OptionButton" & iFrame & "Low"), "Low", .Controls("OptionButton" & iFrame & "Mid"), "Medium", .Controls("OptionButton" & iFrame & "High"), "High", True, "") ' the last condition (i.e.: True, "") ensures not to hit any 'Null' error should all option buttons be unchecked

        Risk = .Controls("TextBox" & iFrame).Value
        Select Case OptionA & Risk
            Case "Medium", "High"
                MsgBox ("Please enter a description of your risk mitigation")
        End Select

        OptionB = Switch(.Controls("OptionButton" & iFrame & "FLow"), "Low", .Controls("OptionButton" & iFrame & "FMid"), "Medium", .Controls("OptionButton" & iFrame & "FHigh"), "High", True, "")

        g = iFrame ' just a guess
        ActiveCell.Offset(g, 15).Value = OptionA
        ActiveCell.Offset(g, 16).Value = Risk
        ActiveCell.Offset(g, 17).Value = OptionB
    Next
End With

【讨论】:

  • 谢谢 Controls 属性正是我想要的
【解决方案2】:

试试这样的。一些快速说明:

1) 我不知道你最后真正想做什么。由于您发布的内容永远不会更改 ActiveCell,因此您要做的就是将最后(第 17 个)值放入 Offset 值中。您应该 avoid using .Activate/.ActiveCell 并使用 Range() 引用,并增加它。

2) 我假设 g 已正确设置为适当的值。

Dim i       As Long
' Create four Arrays to hold the options and risk. Use `1 to 17` instead of just `optionA(16)` 
' (same thing in the end), just for learning purposes and ease of understanding.
Dim optionA(1 To 17), risk(1 To 17), optionB(1 To 17)

' Loop through the array 17 times, checking values each time and saving that value to the array.
For i = LBound(optionA) To UBound(optionB)
    If Frame1.OptionButton1Low Then
        optionA(i) = "Low"
    ElseIf Frame1.OptionButton1Mid Then
        optionA(i) = "Medium"
    ElseIf Frame1.OptionButton1High Then
        optionA(i) = "High"
    End If

    risk(i) = TextBox1.Value
    If (optionA(i) = "Medium" Or optionA(i) = "High") And risk(i) = "" Then
        MsgBox ("Please enter a description of your risk mitigation.")
    End If

    If FrameF1.OptionButton1FLow Then
        optionB(i) = "Low"
    ElseIf FrameF1.OptionButton1FMid Then
        optionB(i) = "Medium"
    ElseIf FrameF1.OptionButton1FHigh Then
        optionB(i) = "High"
    End If

    ActiveCell.Offset(g, 15).Value = optionA(i)
    ActiveCell.Offset(g, 16).Value = risk(i)
    ActiveCell.Offset(g, 17).Value = optionB(i)
Next i

【讨论】:

  • 谢谢,这很好,但我正在努力增加框架和选项按钮旁边的数字,即“FrameX.OptionButtonXLow”,但我真的很喜欢 If/or in the middle that's有帮助。
  • @SeanMcfadzean 你是什么意思?你能详细说明你想通过增加框架和选项按钮旁边的数字来做什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多