在vba excel中使用用户表单更新工作表

yjghlzjz  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(147)

我试图更新的值在表中使用userform的列表框在vba中,但它只更新了一个单元格,而不是所有的单元格,我想更新。任何想法为什么??谢谢提前很多!
按钮单击事件的代码:

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

在用户表单字段上显示列表框值的代码:

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
uoifb46i

uoifb46i1#

您能否确认,调用Refresh_Data()子例程执行了什么操作?可能是

相关问题