简体   繁体   中英

VBA , MS Excel. Update combo box value on change

How do I (on 'Continue' button click) make it pass current string from comboBox@userForm in to excel document cell and close form ?


My current code doesn't work properly. It doesn't update combo box value on change and always return original ("Select subtype") value.

Here's my module code :

Public main As Integer, i As Integer, temp As String, x As String

Public Sub dataValidation()

Dim i As Integer

For i = 3 To 22
    Select Case Cells(i, 6).Value
        Case "Income"
            main = 1
            If Cells(i, 7).Value = "" Then
                Cells(i, 7).Value = getData
            End If
        Case ...
        End Select
Next i

End Sub


Public Function getData()

    UserForm1.Show
    x = UserForm1.cboSubtype.Value
    getData = x

End Function


And my form code:

Private Sub UserForm_Initialize()

With cboSubtype
    '.Value = "Select subtype"'

    Select Case main
        Case 1
            .AddItem "Parents"
            .AddItem "Grant"

        Case 2
            .AddItem "Food"
            .AddItem "Drink"

        Case 3
            .AddItem "Books"
            .AddItem "Fees"

    End Select
End With

End Sub

you may want to adopt these minor modifications

  • Userform code pane

     Private Sub Continue_Click() Me.Hide End Sub Private Sub UserForm_Initialize() With Me.cboSubtype '.Value = "Select subtype"' Select Case mainVal Case 1 .AddItem "Parents" .AddItem "Grant" Case 2 .AddItem "Food" .AddItem "Drink" Case 3 .AddItem "Books" .AddItem "Fees" End Select End With End Sub 
  • Module code

     Option Explicit Public mainVal As Integer Public Sub dataValidation() Dim i As Integer For i = 3 To 22 Select Case Cells(i, 6).value Case "Income" mainVal = 1 If Cells(i, 7).value = "" Then Cells(i, 7).value = getData ' Case ... End Select Next i End Sub Public Function getData() With UserForm4 .Show getData = .cboSubtype.value End With Unload UserForm4 End Function 

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