简体   繁体   English

Excel VBA:基于“组合框选择”然后“求和”将值添加到文本框

[英]Excel VBA: Add Value to TextBox based on ComboBox Selection Then Sum

I'm creating an userform that should allow me to choose an option from a combobox and I need the selection to give a textbox next to it a value because I need to perform a calculation at the end. 我正在创建一个用户窗体,该窗体应该允许我从组合框中选择一个选项,并且我需要该选择才能在其旁边的文本框中提供一个值,因为最后需要执行计算。

I have 4 Frames with 4 comboboxes each and 5 textboxes (the 5th are for the results). 我有4个框架,每个框架有4个组合框和5个文本框(第5个是结果框)。

I'm not sure if this is the proper approach as this is my first work with an userform. 我不确定这是否是正确的方法,因为这是我第一次使用用户窗体。

First, I created a list with "Yes" and "No" in a sheet within the workbook and gave it a namerange. 首先,我在工作簿中的工作表中创建了一个列表,其中包含“是”和“否”,并为其命名。 I thought it would be easier in case I need to add more values in the future without having to modify the code. 我认为这样会更容易,以防将来将来需要添加更多值而不必修改代码。

=OFFSET(Lists!$A$2, 0, 0, COUNTA(Lists!$A:$A),1)

Here's the piece of code I have so far. 这是我到目前为止的代码。

Private Sub UserForm_Initialize()

Dim rngResponse As Range
Dim ws As Worksheet

Set ws = Worksheets("Lists")

For Each rngResponse In ws.Range("Response")

Me.cbResponse1.AddItem rngResponse.Value
Me.cbResponse2.AddItem rngResponse.Value
Me.cbResponse3.AddItem rngResponse.Value
Me.cbResponse4.AddItem rngResponse.Value
Me.cbResponse5.AddItem rngResponse.Value
Me.cbResponse6.AddItem rngResponse.Value
Me.cbResponse7.AddItem rngResponse.Value
Me.cbResponse8.AddItem rngResponse.Value
Me.cbResponse9.AddItem rngResponse.Value
Me.cbResponse10.AddItem rngResponse.Value
Me.cbResponse11.AddItem rngResponse.Value
Me.cbResponse12.AddItem rngResponse.Value
Me.cbResponse13.AddItem rngResponse.Value
Me.cbResponse14.AddItem rngResponse.Value
Me.cbResponse15.AddItem rngResponse.Value
Me.cbResponse16.AddItem rngResponse.Value

Next rngResponse

End Sub

Each textbox is named as follows. 每个文本框的名称如下。

txtResponse1
txtResponse2
txtResponse3
txtResponse4

until txtResponse16 直到txtResponse16

Then the textboxes were I want to display the results in each frame are 然后我要在每个帧中显示结果的文本框是

txtResult1
txtResult2
txtResult3
txtResult4

I need the "Yes" value to be worth 1 and the "No" value to be worth 0 and then SUM all the values in the frame and divide them by the count of responses. 我需要将“是”值设为1,将“否”值设为0,然后将帧中的所有值相加,然后将它们除以响应计数。 So if a cbbox is blank then the txtbox should also be blank. 因此,如果cbbox为空白,则txtbox也应该为空白。

Any inputs or suggestions are very much welcomed. 任何意见或建议都非常欢迎。

EDIT: 编辑:

I added one of this for each comboxbox and it works but now I have 16 subs. 我为每个comboxbox添加了其中一个,它可以工作,但是现在我有16个subs。 Is there a way to shorten this? 有什么办法可以缩短时间?

Private Sub cbResponse1_Change()

If Me.cbResponse1.Value = "" Then
Me.txtResponse1.Value = ""
End If

If Me.cbResponse1.Value = "Yes" Then
Me.txtResponse1.Value = 1
End If

If Me.cbResponse1.Value = "No" Then
Me.txtResponse1.Value = 0
End If

End Sub

You could cut down your code quite a bit by having one function that all the event handlers called, something like this: 您可以通过拥有一个所有事件处理程序都调用的函数来大幅度削减代码,如下所示:

Private Sub cbResponse1_Change()
    Call cbResponsex_Change(Me.cbResponse1, Me.txtResponse1)
End Sub

Private Sub cbResponse2_Change()
    Call cbResponsex_Change(Me.cbResponse2, Me.txtResponse2)
End Sub

Private Sub cbResponsex_Change(cbResponse As Combobox, txtResponse As TextBox)

    If cbResponse.Value = "" Then
        txtResponse.Value = ""
    End If

    If cbResponse.Value = "Yes" Then
        txtResponse.Value = 1
    End If

    If cbResponse.Value = "No" Then
        txtResponse.Value = 0
    End If

End Sub

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

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