简体   繁体   中英

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).

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

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. So if a cbbox is blank then the txtbox should also be blank.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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