UserForm w / ComboBox - 根据所选选项输入数据库值

时间:2016-02-26 18:41:51

标签: vba excel-vba combobox userform excel

我有一个UserForm用于输入调查数据,有32个ComboBox。每个ComboBox都有相同的响应集,每个响应都有相应的分数。当用户点击Save时,而不是将响应保存到数据库中,我希望得分。以下是分数:

Muy en desacuerdo:-2 En desacuerdo:-1 Ni de acuerdo / en desacuerdo:0 De acuerdo:1 Muy de acuerdo:2

然而,在某些情况下,回答是反向评分的(因此" muy de acuerdo"会得到-2而不是2)。

之前有人给了我一个循环的代码,并为每个ComboBox分配了正确的选项列表:     comboItems()=数组(" Muy en desacuerdo",_                      " En desacuerdo",_                      " Ni de acuerdo / en desacuerdo",_                      " De acuerdo",_                      " Muy de acuerdo")

For Each ct In Me.Controls

If TypeName(ct) = "ComboBox" And _
    ct.Name <> "cboGender" And _
    ct.Name <> "cboDepartment" Then

    For i = LBound(comboItems) To UBound(comboItems)
        ct.AddItem comboItems(i)
    Next i

End If

Next ct

然后,这是将每个字段添加到数据库的代码示例:

Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("CRUDO")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
ws.Unprotect ""
    .Cells(lRow, 1).Value = Me.txtFecha.Value
    .Cells(lRow, 2).Value = Me.txtTime.Value
    .Cells(lRow, 3).Value = Me.txtPlace.Value
    .Cells(lRow, 4).Value = Me.cboDepartment.Value
    .Cells(lRow, 5).Value = Me.txtMunicipality.Value
    .Cells(lRow, 6).Value = Me.txtGroupName.Value
    .Cells(lRow, 7).Value = Me.txtComponent.Value
    .Cells(lRow, 8).Value = Me.txtComments.Value
    .Cells(lRow, 9).Value = Me.txtName.Value
    .Cells(lRow, 10).Value = Me.txtBirthDate.Value
    .Cells(lRow, 11).Value = Me.cboGender.Value
    .Cells(lRow, 12).Value = Me.cboAE1A.Value
    .Cells(lRow, 13).Value = Me.cboAE2A.Value
    .Cells(lRow, 14).Value = Me.cboAE3A.Value
    .Cells(lRow, 15).Value = Me.cboAE4A.Value
    .Cells(lRow, 16).Value = Me.cboAE5A.Value

因此,在上面的示例中,我们假设cboAE1A根据上述比例正常评分。然而,AE2A和AE5A是反向评分。

这可能吗?最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

首先,为了保存分数而不是实际的响应,那么您应该将代码更改为:

.Cells(lRow, 1).Value = Me.txtFecha.ListIndex -2

如果你想反转AE2A的分数:

.Cells(lRow, 2).Value = -(Me.txtTime.ListIndex -2)