VBA从sub返回一个值

时间:2013-06-13 00:03:57

标签: vb.net vba ms-word return-value

我基本上想要一个调查文档,其中用户选择评级,例如“始终”和“从不”。他们通过点击单选按钮选择评级。评分被加权并被平均为总分,如GPA,即总值3,有些值2,每个问题应加上然后平均。以下是我的一个问题代码的一部分。

如何在选择按钮后将值放在最后并将其平均所有问题?

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged

    If RadioButton1.Checked = True Then

        RadioButton3.Checked = False
        RadioButton2.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        num1 = 2
    End If

End Sub

Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
    If RadioButton2.Checked = True Then

        RadioButton3.Checked = False
        RadioButton1.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        num1 = 3
    End If
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
    If RadioButton3.Checked = True Then

        RadioButton1.Checked = False
        RadioButton2.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        num1 = 1
    End If
End Sub

Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
    If RadioButton4.Checked = True Then

        RadioButton3.Checked = False
        RadioButton2.Checked = False
        RadioButton1.Checked = False
        RadioButton5.Checked = False
        num1 = 0
    End If
End Sub

Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged


    If RadioButton5.Checked = True Then

        RadioButton3.Checked = False
        RadioButton2.Checked = False
        RadioButton4.Checked = False
        RadioButton1.Checked = False
        num1 = 0
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

首先,您可以将所有这些RadioButton事件处理程序合并到一个处理程序中。此外,您不必“取消选中”组中的其他RadioButton,因为这将自动为您完成。最后,创建一个单独的方法来计算评级并更新表单,然后在处理程序的底部调用该方法。

这可能看起来像这样:

Private num1 As Integer

Private Sub set1_CheckedChanged(sender As Object, e As EventArgs) Handles _
        RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, _
        RadioButton4.CheckedChanged, RadioButton5.CheckedChanged

    If RadioButton1.Checked Then
        num1 = 2
    ElseIf RadioButton2.Checked Then
        num1 = 3
    ElseIf RadioButton3.Checked Then
        num1 = 1
    ElseIf RadioButton4.Checked Then
        num1 = 0
    ElseIf RadioButton5.Checked Then
        num1 = 0
    End If

    UpdateRating()
End Sub

Private Sub UpdateRating()
    ' ... compute the rating using "num1", "num2", "num3", etc ...
    ' ... then update the GUI with the new value ...
End Sub

请注意,所有五个RadioButton都列在“Handles”关键字之后,使它们全部触发相同的处理程序。您可以为每组RadioButtons执行类似的操作...只需确保每组RadioButton都在其 OWN 容器中,如GroupBox或Panel。 RadioButton与其自己的容器中的所有其他RadioButttons互斥。

答案 1 :(得分:0)

我对你的情况不太确定..但你可以试试这个

声明你的var将计算所有结果..

Dim num1 as Integer 
Dim num2 as Integer '--> for other group radiobuttons
Dim num3 as Integer '--> for other group radiobuttons

Dim nTotal as Integer
Dim nAvg as Integer

每个 checkchanged

中执行此操作
Private Sub RadioButton_CheckedChanged(..) Handles RadioButton1_CheckedChanged, RadioButton2_CheckedChanged,  .....

  'codes
  countit()

End Sub

Sub CountIt()
   nTotal = num1 + num2 + num3
   nAvg = (num1 + num2 + num3) / 3
End Sub