简体   繁体   English

EXCEL VBA:如何在按下命令按钮后将单选按钮重置为默认选择?

[英]EXCEL VBA: How can I reset a radio button to a default selection upon pressing a Command Button?

I am having some difficulties trying to determine how I can reset a radio button to "OptionButton1" when I click "CommandButton1". 我在尝试确定单击“ CommandButton1”时如何将单选按钮重置为“ OptionButton1”时遇到一些困难。

Here is my coding for the buttons (following is listed in "Sheet3" code): 这是我对按钮的编码(以下内容在“ Sheet3”代码中列出):

Private Sub CommandButton1_Click()
    ' MED Tab - Sort Button
    ' Returns view back to default, then sort data
    On Error Resume Next
    Call Medical.MED_Sort
End Sub

Private Sub OptionButton1_Click()
    ' MED Tab -
    On Error Resume Next
    ActiveSheet.AutoFilterMode = False
End Sub

Private Sub OptionButton2_Click()
    ' MED Tab - Show Verified Only
    On Error Resume Next
    ActiveSheet.AutoFilterMode = False
    Call Medical.VRFD_ONLY
    Call Module1.FirstVisibleCell
End Sub

Private Sub OptionButton3_Click()
    ' MED Tab - Show NO Scores
    On Error Resume Next
    ActiveSheet.AutoFilterMode = False
    Call Medical.No_Scores
    Call Module1.FirstVisibleCell
End Sub

Private Sub OptionButton4_Click()
    ' MED Tab - Show With Scores
    On Error Resume Next
    ActiveSheet.AutoFilterMode = False
    Call Medical.With_Scores
    Call Module1.FirstVisibleCell
End Sub

and here is the Macro for the "CommandButton1" (in the "Medical" Module): 这是“ CommandButton1”的宏(在“医疗”模块中):

Sub MED_Sort()
'
' MED_Sort Macro
'

'

        ActiveSheet.AutoFilterMode = False

    Columns("A:I").Select
    ActiveWorkbook.Worksheets("Med").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Med").Sort.SortFields.Add Key:=Range("H:H"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Med").Sort.SortFields.Add Key:=Range("F:F"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Med").Sort.SortFields.Add Key:=Range("D:D"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Med").Sort.SortFields.Add Key:=Range("B:B"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Med").Sort
        .SetRange Range("A:I")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

So in the first code block, you can see that basically the OptionButton1's job is literally just to turn off any Auto Filters (so clicking this radio button is basically a reset). 因此,在第一个代码块中,您可以看到基本上OptionButton1的工作实际上是关闭所有自动过滤器(因此,单击此单选按钮基本上是一个重置)。
Also, the CommandButton1 is an AutoFilter reset as well, but it will sort all the data to my default sorting preferences. 同样,CommandButton1也是自动筛选器重置,但是它将所有数据按照我的默认排序首选项进行排序。 Because CommandButton1 basically does what OptionButton1 does, plus it also sorts, when I press the CommandButton1, I need it to also automatically select OptionButton1 in the event any other buttons are already selected. 因为CommandButton1基本上可以执行OptionButton1的工作,而且还可以排序,所以当我按下CommandButton1时,如果已经选择了其他任何按钮,我还需要它自动选择OptionButton1。

Davis, You can use OptionButton1.Value = 1 to programably select one radio button or the other. 戴维斯,您可以使用OptionButton1.Value = 1来以编程方式选择一个单选按钮或另一个。 If I've understood you question correctly, this should do it for you. 如果我理解您的问题正确,那么应该为您解决。 If I've misunderstood you, please let me know. 如果我误解了您,请告诉我。

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

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