我正在编写带有宏用户表单的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对帧,在userform上还有17对。所有文本框,框架,选项按钮和字符串都按顺序命名。所以现在我可以再复制并粘贴它17次并得到我想要的结果。
我想知道是否有办法编写改变数字的Do While循环,因此Option1A变为Option2A,Option3A和OptionButton1FLow变为OptionButton2FLow等。基本上改变所有数字1中的数字1代码为x值。
我的研究中没有找到任何建议,我开始认为这是不可能的,
非常感谢任何见解,
提前干杯,
肖恩
答案 0 :(得分:0)
尝试这样的事情。一些快速说明:
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
答案 1 :(得分:0)
使用class
对象的Controls
属性按名称对其所有成员(如Frames,TextBoxes等)进行寻址
此外,您可以使用UserForm
功能缩短选项按钮值检查和相应的值
Switch()