簡體   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