我在Excel中有一个用户表单,它有一个从动态范围(5列,1是唯一ID号)中提取数据的列表框。
当用户单击记录时,它会在文本框中显示每个值,并在标签中显示ID号。
Private Sub ListBox1_Click()
Me.Label6 = Me.ListBox1.List(ListBox1.ListIndex, 0) 'record number
Me.TextBox1 = Me.ListBox1.List(ListBox1.ListIndex, 4)'Staff name
Me.TextBox2 = Me.ListBox1.List(ListBox1.ListIndex, 1)'Strategy
Me.TextBox3 = Me.ListBox1.List(ListBox1.ListIndex, 2)'Notes
Me.TextBox4 = Me.ListBox1.List(ListBox1.ListIndex, 3)'Date
End Sub我希望用户对记录进行更改,并更新列表框和工作表上的数据。它只用一个值更新正确的记录!
User form with updated same value in all columns see rec# 5
Private Sub CMDUpdate_Click()
Dim erowa As Integer
Dim x As Integer
erowa = Application.WorksheetFunction.CountA(Sheet9.Range("A:A"))
For x = 2 To erowa
If Sheet9.Cells(x, "A").Value = Me.Label6 Then 'this is the unique ID
Sheet9.Cells(x, "B").Value = Me.TextBox1.Value 'Staff name
Sheet9.Cells(x, "C").Value = Me.TextBox2.Value 'Strategy
Sheet9.Cells(x, "D").Value = Me.TextBox3.Value 'Notes
Sheet9.Cells(x, "E").Value = Me.TextBox4.Value 'date
End If
Next
End Sub由于某些原因,日期格式也会显示Julian值!
我花了一整天的时间研究,看教程,读博客。
发布于 2020-01-31 04:28:46
这是一种在Me.Label6中查找值并更新其旁边单元格的方法
Private Sub CMDUpdate_Click()
Dim cell As Range
Set cell = sheet9.Range("A:A").Find(What:=Me.Label6.Caption, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
MatchCase:=False)
If Not cell Is Nothing Then
' Column B
cell.Offset(, 1).Value = Me.TextBox1.Value 'Staff name
' Column C
cell.Offset(, 2).Value = Me.TextBox2.Value 'Strategy
' Column D
cell.Offset(, 3).Value = Me.TextBox3.Value 'Notes
' Column E
cell.Offset(, 4).Value = Me.TextBox4.Value 'date
Else
MsgBox "Record: " & Me.Label6.Caption & " not found in column A!"
End If
End Sub如果有效,请让我知道!
https://stackoverflow.com/questions/59992339
复制相似问题