【发布时间】:2021-04-13 14:14:23
【问题描述】:
我创建了一个用户输入表单,它获取表格中的数据并将其显示为 VBA 表单中的列表框。我有两个切换按钮,可以循环显示数据。我有文本框/组合框在表单上显示该数据。
我无法让表单更新数据。当表单中的数据发生更改时,我设法让第一列更新,但我无法让其他任何工作。所以主要是cmdUpdate_Click sub中的代码。
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdRefresh_Click()
Dim x As Integer
For x = 0 To Me.lstMyData.ListCount - 1
If Me.lstMyData.Selected(x) Then Me.lstMyData.Selected(x) = False
Next x
End Sub
Private Sub ComboBox1_DropButtonClick()
microParaNameButt Me.ComboBox1
End Sub
'Private Sub ComboBox1_Change()
' microParaName Me.ComboBox1
'End Sub
Private Sub lstMyData_Click()
Dim i As Integer
i = Me.lstMyData.ListIndex
Me.lstMyData.Selected(i) = True
Me.TextBox1.Value = Me.lstMyData.Column(0, i)
Me.ComboBox1.Value = Me.lstMyData.Column(0, i)
Me.TextBox2.Value = Me.lstMyData.Column(1, i)
Me.ComboBox2.Value = Me.lstMyData.Column(1, i)
Me.TextBox3.Value = Me.lstMyData.Column(2, i)
Me.ComboBox3.Value = Me.lstMyData.Column(2, i)
Me.TextBox4.Value = Me.lstMyData.Column(3, i)
Me.ComboBox4.Value = Me.lstMyData.Column(3, i)
Me.TextBox5.Value = Me.lstMyData.Column(4, i)
Me.ComboBox5.Value = Me.lstMyData.Column(4, i)
Me.TextBox6.Value = Me.lstMyData.Column(5, i)
Me.ComboBox6.Value = Me.lstMyData.Column(5, i)
Me.TextBox7.Value = Me.lstMyData.Column(6, i)
Me.ComboBox7.Value = Me.lstMyData.Column(6, i)
Me.TextBox8.Value = Me.lstMyData.Column(7, i)
Me.ComboBox8.Value = Me.lstMyData.Column(7, i)
Me.TextBox9.Value = Me.lstMyData.Column(8, i)
Me.ComboBox9.Value = Me.lstMyData.Column(8, i)
Me.TextBox10.Value = Me.lstMyData.Column(9, i)
Me.ComboBox10.Value = Me.lstMyData.Column(9, i)
End Sub
Private Sub ToggleButton1_Click()
Dim i As Integer
i = Me.lstMyData.ListIndex
Me.lstMyData.Selected(i) = True
Me.TextBox1.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox1.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox2.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox2.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox3.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox3.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox4.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox4.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox5.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox5.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox6.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox6.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox7.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox7.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox8.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox8.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox9.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox9.Value = Me.lstMyData.Column(0, i - 1)
Me.TextBox10.Value = Me.lstMyData.Column(0, i - 1)
Me.ComboBox10.Value = Me.lstMyData.Column(0, i - 1)
Me.lstMyData.Selected(i - 1) = True
End Sub
Private Sub ToggleButton2_Click()
Dim i As Integer
i = Me.lstMyData.ListIndex
Me.lstMyData.Selected(i) = True
Me.TextBox1.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox1.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox2.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox2.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox3.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox3.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox4.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox4.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox5.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox5.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox6.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox6.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox7.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox7.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox8.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox8.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox9.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox9.Value = Me.lstMyData.Column(0, i + 1)
Me.TextBox10.Value = Me.lstMyData.Column(0, i + 1)
Me.ComboBox10.Value = Me.lstMyData.Column(0, i + 1)
Me.lstMyData.Selected(i + 1) = True
End Sub
Private Sub UserForm_Initialize()
Dim x As Integer
For x = 0 To Me.lstMyData.ListCount - 1
If Me.lstMyData.Selected(c) Then Me.lstMyData.Selected(x) = False
Next x
End Sub
Private Sub cmdUpdate_Click()
Dim rowslect As Double
Dim i As Integer
Dim wb As Workbook
Dim ws As Worksheet
Set ws = Worksheets("Sheet3")
i = Me.lstMyData.ListIndex
rowselect = i + 1
With ws
.Cells((i + 2), 1).Value = Me.TextBox1.Value
.Cells((i + 2), 2).Value = Me.TextBox2.Value
.Cells((i + 2), 3).Value = Me.TextBox3.Value
.Cells((i + 2), 4).Value = Me.TextBox4.Value
.Cells((i + 2), 5).Value = Me.TextBox5.Value
.Cells((i + 2), 6).Value = Me.TextBox6.Value
.Cells((i + 2), 7).Value = Me.TextBox7.Value
.Cells((i + 2), 8).Value = Me.TextBox8.Value
.Cells((i + 2), 9).Value = Me.TextBox9.Value
.Cells((i + 2), 10).Value = Me.TextBox9.Value
End With
End Sub
【问题讨论】:
-
在您的
cmdUpdate_Click子程序中设置断点或Debug.Print语句,并仔细检查文本框中的值。有时,表单上可见的值不是代码中访问的值。查看this answer 进行有趣的讨论。 -
嘿@PeterT - 感谢您的信息!我已将 Debug.Print 语句添加到子程序中,并且我可以看到其他文本框值没有被彻底提取。我可以更改表格第一列中的数据,但其他一切似乎都不起作用。我将 .value 更改为 .text,但没有任何区别
-
为什么要为每个项目同时填充文本框和组合框?而且您的代码在某些地方看起来很奇怪:例如。
lstMyData_Click和ToggleButton1_Click大概使用不同的代码来访问相同的数据?lstMyData最初是如何填充的?