简体   繁体   中英

Update worksheet using userform in vba excel

I am trying to update values of row in table using userform's listbox in vba but, it only update only one of the cells not all the cells i want to update. Any ideas why?? Thanks a lot in advance!!

the code for the click event of the button:

Private Sub Update_Click()
    ' textbox11 keeps the id (number) of the row
    If Me.TextBox11.Value = "" Then
        MsgBox "Error"
        Exit Sub
    End If
    
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("Sheet1")
    Dim selected_row As Long
    selected_row = Application.WorksheetFunction.Match(CLng(Me.TextBox11.Value), sh.Range("A:A"), 0)
    
    sh.Range("B" & selected_row).Value = Me.ComboBox1.Value
    sh.Range("C" & selected_row).Value = Me.TextBox1.Value
    sh.Range("D" & selected_row).Value = Me.TextBox2.Value
    sh.Range("O" & selected_row).Value = Me.TextBox3.Value
    sh.Range("S" & selected_row).Value = Me.TextBox4.Value
    sh.Range("Y" & selected_row).Value = Me.TextBox6.Value
    sh.Range("U" & selected_row).Value = Me.TextBox7.Value
    sh.Range("E" & selected_row).Value = Me.ComboBox2.Value
    sh.Range("T" & selected_row).Value = Me.ComboBox3.Value
    sh.Range("V" & selected_row).Value = Me.ComboBox4.Value
    sh.Range("Z" & selected_row).Value = Me.ComboBox5.Value
    sh.Range("AA" & selected_row).Value = Me.ComboBox6.Value
    
    ' clear contents of userform items after update
    Me.ComboBox1.Value = ""
    Me.TextBox1.Value = ""
    Me.TextBox2.Value = ""
    Me.TextBox11.Value = ""
    Me.TextBox12.Value = ""
    Me.ComboBox2.Value = ""
    Me.TextBox3.Value = ""
    Me.TextBox4.Value = ""
    Me.ComboBox3.Value = ""
    Me.TextBox6.Value = ""
    Me.TextBox7.Value = ""
    Me.ComboBox4.Value = ""
    Me.ComboBox5.Value = ""
    Me.ComboBox6.Value = ""

    Call Refresh_Data
End Sub

the code to show the values of the listbox on the userform's fields:

Private Sub ListBox1_Click()
    Me.TextBox11.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
    Me.ComboBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
    Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
    Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 3)
    Me.ComboBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 4)
    Me.TextBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 13)
    Me.TextBox6.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 24)
    Me.TextBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 18)
    Me.ComboBox3.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 19)
    Me.TextBox7.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 20)
    Me.ComboBox4.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 21)
    Me.ComboBox5.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 25)
    Me.ComboBox6.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 26)
    Me.TextBox12.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
End Sub

Can You please confirm, What action does Call Refresh_Data() Sub routine perform? May be it is

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