简体   繁体   English

将VBA表单中的单选值插入Excel电子表格

[英]Insert radio values from VBA form into Excel spreadsheet

How do you insert selected radio values into a Excel spreadsheet from a VBA form? 如何从VBA表单将选定的单选值插入Excel电子表格? By radio values we mean that there only one possible option. 无线电值是指只有一种可能的选择。 I'm struggling to achieve this in VBA. 我正在努力在VBA中实现这一目标。


Here are my radio values: 这是我的收音机值:

priority - options located in the frame fr_Priority , two possible checkbox options: 优先级-位于框架fr_Priority中的选项,两个可能的复选框选项:

  • priority_y - If user selects this then Yes is inserted into cell priority_y-如果用户选择此选项,则将是”插入到单元格中

  • priority_n - If user selects this then No is inserted into cell priority_n-如果用户选择此选项,则将“ 否”插入到单元格中

lectureStyle - options located in the frame fr_LectureStyle , two possible checkbox options: tutorialStyle-位于框架fr_LectureStyle中的选项,两个可能的复选框选项:

  • lecturestyle_trad - If user selects this then Traditional is inserted into cell tutorialstyle_trad-如果用户选择此选项,则将Traditional(传统 )插入到单元格中

  • lecturestyle_sem - If user selects this then Seminar is inserted into cell tutorialstyle_sem-如果用户选择此选项,则将Seminar插入单元格中

  • lecturestyle_lab - If user selects this then Lab is inserted into cell tutorialstyle_lab-如果用户选择此选项,则将Lab插入单元格

  • lecturestyle_any - If user selects this then Any is inserted into cell Lessonstyle_any-如果用户选择此选项,则将Any插入到单元格中

roomStructure - options located in the frame fr_roomStruc , two possible checkbox options: roomStructure-位于框架fr_roomStruc中的选项,两个可能的复选框选项:

  • rs_Tiered - If user selects this then Tiered is inserted into cell rs_Tiered-如果用户选择此选项,则将Tiered插入到单元格中

  • rs_Flat - If user selects this then Flat is inserted into cell rs_Flat-如果用户选择此选项,则将Flat插入到单元格中


Here is my code so far: 到目前为止,这是我的代码:

Private Sub btnSubmit_Click()
Dim ws As Worksheet, rng1 As Range
Set ws = Worksheets("main")

' Get last empty cell in column A
  Set rng1 = ws.Cells(Rows.Count, "a").End(xlUp)

' I'm strugging to insert the radio values inserted at this point, they 
' need to go into the cells specified below

' rng1.Offset(1, 10) = priority
' rng1.Offset(1, 11) = lectureStyle
' rng1.Offset(1, 12) = roomStructure

End Sub

Thanks so much for the help! 非常感谢你的帮助!

To me it just seems like you need to do some validation on your radio box's values, here is one way you could do it 对我来说,似乎您需要对单选框的值进行一些验证,这是您可以执行的一种方法

 Private Sub btnSubmit_Click() Dim ws As Worksheet, rng1 As Range Set ws = Worksheets("main") 'Get last empty cell in column A Set rng1 = ws.Cells(Rows.Count, "a").End(xlUp) ' I'm strugging to insert the radio values inserted at this point, they ' need to go into the cells specified below rng1.Offset(1, 10) = GetPriority End Sub Private Function GetPriority() As String Dim priority As String If Me.priority_y.Value = True Then priority = "Yes" ElseIf Me.priority_n.Value = True Then priority = "No" Else priority = "N/A" End If GetPriority = priority End Function 
 Private Sub btnSubmit_Click() Dim ws As Worksheet, rng1 As Range Set ws = Worksheets("main") 'Get last empty cell in column A Set rng1 = ws.Cells(Rows.Count, "a").End(xlUp) ' I'm strugging to insert the radio values inserted at this point, they ' need to go into the cells specified below rng1.Offset(1, 10) = GetPriority End Sub Private Function GetPriority() As String Dim priority As String If Me.priority_y.Value = True Then priority = "Yes" ElseIf Me.priority_n.Value = True Then priority = "No" Else priority = "N/A" End If GetPriority = priority End Function 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM