框架中包含OptionButton的ExcelUserForm-用于标识和使用所选OptionButton编号的代码

rdlzhqv9  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(92)
`Private Sub CommandButton1_Click()
'I have a UserForm that includes 2 Frames and 2 Command Buttons.
'The 2 Frames each hold 30 OptionButtons;
'           Frame1 has OptionButtons 1 through 30 and allows the user to
'           select which, of 30 bank accounts is the origin in a money transfer,
'           and Frame2 has 31 through 60 and similarly decides the destination Account in the transfer.
'The 2 CommandButtons are to execute the same idea, but in two different ways. I only need one.
'This code below is meant to display the name of the Range that is becoming FromAcc,
'           which is: ("LedgerTable" & [the Number of the OptionButton selected]).
'    The MsgBox is just for testing to make sure I get this part right;
'    it'll eventually get replaced with more code to execute.
'Many statements are commented-out because, -who wants to include ALL the buttons for just a test?  Right?

Dim FromAcc As Range

FromAcc = "No OptionButton has been selected."
'The FromAcc begins as that text because if, at the end, no OptionButton has been selected,
'then the MsgBox, whose message is determined by the value of FromAcc, is all prepared with the right message.
'So, I go off thinkin' I'm all clever, right?
'      ...Well then I thought I was really hot stuff when 
'              I came about the second method via CommandButton2. Watch.

'All of these statements check each individual OptionButton and set FromAcc as its own unique textstring.
'These textstrings are recognized Table names; Named Ranges within the Workbook.

If Frame1.OptionButton1.Value = True Then FromAcc = "LedgerTable1"
If Frame1.OptionButton2.Value = True Then FromAcc = "LedgerTable2"
If Frame1.OptionButton3.Value = True Then FromAcc = "LedgerTable3"
If Frame1.OptionButton4.Value = True Then FromAcc = "LedgerTable4"
If Frame1.OptionButton5.Value = True Then FromAcc = "LedgerTable5"
If Frame1.OptionButton6.Value = True Then FromAcc = "LedgerTable6"
'If Frame1.OptionButton7.Value = True Then FromAcc = "LedgerTable7"
'If Frame1.OptionButton8.Value = True Then FromAcc = "LedgerTable8"
'If Frame1.OptionButton9.Value = True Then FromAcc = "LedgerTable9"
'If Frame1.OptionButton10.Value = True Then FromAcc = "LedgerTable10"
'If Frame1.OptionButton11.Value = True Then FromAcc = "LedgerTable11"
'If Frame1.OptionButton12.Value = True Then FromAcc = "LedgerTable12"
'If Frame1.OptionButton13.Value = True Then FromAcc = "LedgerTable13"
'If Frame1.OptionButton14.Value = True Then FromAcc = "LedgerTable14"
'If Frame1.OptionButton15.Value = True Then FromAcc = "LedgerTable15"
'If Frame1.OptionButton16.Value = True Then FromAcc = "LedgerTable16"
'If Frame1.OptionButton17.Value = True Then FromAcc = "LedgerTable17"
'If Frame1.OptionButton18.Value = True Then FromAcc = "LedgerTable18"
'If Frame1.OptionButton19.Value = True Then FromAcc = "LedgerTable19"
'If Frame1.OptionButton20.Value = True Then FromAcc = "LedgerTable20"
'If Frame1.OptionButton21.Value = True Then FromAcc = "LedgerTable21"
'If Frame1.OptionButton22.Value = True Then FromAcc = "LedgerTable22"
'If Frame1.OptionButton23.Value = True Then FromAcc = "LedgerTable23"
'If Frame1.OptionButton24.Value = True Then FromAcc = "LedgerTable24"
'If Frame1.OptionButton25.Value = True Then FromAcc = "LedgerTable25"
'If Frame1.OptionButton26.Value = True Then FromAcc = "LedgerTable26"
'If Frame1.OptionButton27.Value = True Then FromAcc = "LedgerTable27"
'If Frame1.OptionButton28.Value = True Then FromAcc = "LedgerTable28"
'If Frame1.OptionButton29.Value = True Then FromAcc = "LedgerTable29"
'If Frame1.OptionButton30.Value = True Then FromAcc = "LedgerTable30"
MsgBox (FromAcc.Value)

'...and so, this code doesn't work right, but I think you get the idea of what I'm trying to do.
'But I'm trying!!  The next bit of code, for CommandButton2, tries to do the same thing,
'but through a different method. But I think you can see how the CommandButton1-method is simple and direct,
'whereas the second method is rather efficient.  I'm trying for efficient, not simple.  Help please?

End Sub

Private Sub CommandButton2_Click()
'This code tries to use a counter to cycle through the number attached to each of the OptionButtons,
'and check the value for True or not. When it comes to set the value of FromAcc,
'the same counter is added to the text "LedgerTable", which makes the names of the recognized Tables.
Dim i As Integer
Dim FromAcc As Range
Dim ToAcc As Range

For i = 1 To 6 ' (30)
If Frame1.OptionButton & i.Value = True Then FromAcc = "LedgerTable" & i
Next i

'For i = 31 To 60
'If Frame2.OptionButton & i.Value = True Then FromAcc = "LedgerTable" & i
'Next i

MsgBox ("FromAcc:" & FromAcc.Value)   ' (  ... & "      ToAcc:" & ToAcc)

'...See? Completely more efficient, right?  It doesn't work either.
'Do I have to do some fancy thing like,
'copy the Caption of the OptionButton and strip away the "OptionButton" text 
'so the number is the only thing left?
'What 's really annoying is that just when I think I'm getting the hang of it (writing code),
'I come to find that I'm really not. Help? Please? Anyone? -Thanks in advance.
End Sub
`

在此键入
我写了上面的代码,但我不太擅长语法,所以我很确定它需要比我更好的眼光。当我学习Excel的一个新函数时,我总是感到惊讶。所以,也许有一个比我想的更好、更直接/更有效的方法。难道没有一个简单的函数,它只返回在特定框架内选择的任何东西的名称吗?
我还尝试了代码的修改版本:Is is possible to grab the number of frames in a userform and the number of optionbuttons in a frame?但是当我改变MsgBox时,有些东西搞砸了。

`Private Sub CommandButton3_Click() ' I changed the name from "XX()". I've noted all the changes I made.

Dim c As Control, n As Long ' I changed the variable from nl to n.
Dim FromAcc As Range ' I added this line.

For Each c In UserForm1.Frame1.Controls
    If TypeOf c Is msforms.OptionButton Then
        n1 = n1 + 1
'There was an unnecessary line of code here that I deleted with no loss.
    End If
Next c

' this original line works: MsgBox " option button number = " & n
'my modified version does not:
FromAcc = "LedgerTable" & n
MsgBox = FromAcc.Value

End Sub`
3ks5zfa0

3ks5zfa01#

您在此处引用选项按钮的方式

For i = 1 To 6 ' (30)
If Frame1.OptionButton & i.Value = True Then FromAcc = "LedgerTable" & i
Next i

错误。您正在连接Frame1.OptionButton变量(不存在)的内容和i变量。
若要使代码正常工作,可以将“OptionButton”字符串与i变量连接起来,并尝试在Frame1.Controls集合中查找具有该名称的控件,如下所示:

Private Sub CommandButton1_Click()
    Dim FromAcc As String
    FromAcc = "No option has been selected"
    For i = 1 To 4 ' (30)
        If Frame1.Controls("OptionButton" & i) = True Then FromAcc = "LedgerTable" & i
    Next i
    
    MsgBox FromAcc
End Sub

但是,如果找不到“OptionButton”& i对象,则会崩溃。您可以实现一些错误处理,或者尝试类似下面的操作,效果应该一样:

Private Sub CommandButton1_Click()
    Dim FromAcc As String
    FromAcc = "No option has been selected"
    
    Dim c As Control
    For Each c In Frame1.Controls
        If TypeName(c) = "OptionButton" And c.Value = True Then
            FromAcc = "LedgerTable" & Mid(c.Name, 13, Len(c.Name) - 12)
            Exit For
        End If
    Next c
    MsgBox FromAcc
End Sub

要使这项工作,你必须保持命名约定的“OptionButton”+数字,但似乎你已经这样做了,所以一切都应该是好的。

相关问题