繁体   English   中英

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

[英]Update worksheet using userform in vba excel

我正在尝试使用 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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