有没有一种方法可以更新用户表单以在列表框中进行更新

时间:2019-09-05 18:44:28

标签: excel vba

谢谢。

我有一个包含ListBox1的UserForm1。 ListBox1显示excel工作表中的数据范围。

如果我单击ListBox1中的数据,则会弹出Userform2及其相应的数据。

从本质上讲,如果用户要编辑ListBox1中的数据,则他们将双击作为UserForm2弹出的数据,并双击CommandButton1作为保存功能。然后,应将其与更改一起显示在ListBox1中。

我遇到的问题是,如果我在UserForm2中编辑数据并单击CommandButton1,它不会反映在我的ListBox1中,并且会给我错误代码“运行时错误'70':无法设置列属性。权限被拒绝

我对于ListBox1的代码如下:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm2.TextBox9.Text = Me.ListBox1.Column(0)
UserForm2.TextBox1.Text = Me.ListBox1.Column(1)
UserForm2.TextBox2.Text = Me.ListBox1.Column(2)
UserForm2.TextBox3.Text = Me.ListBox1.Column(3)
UserForm2.TextBox4.Text = Me.ListBox1.Column(4)
UserForm2.TextBox5.Text = Me.ListBox1.Column(5)
UserForm2.TextBox6.Text = Me.ListBox1.Column(6)
UserForm2.TextBox7.Text = Me.ListBox1.Column(7)
UserForm2.TextBox8.Text = Me.ListBox1.Column(8)
UserForm2.Show
End Sub

我对CommandButton1的代码如下:

Private Sub CommandButton1_Click()

UserForm1.ListBox1.Column(0) = Me.TextBox9.Text
UserForm1.ListBox1.Column(1) = Me.TextBox1.Text
UserForm1.ListBox1.Column(2) = Me.TextBox2.Text
UserForm1.ListBox1.Column(3) = Me.TextBox3.Text
UserForm1.ListBox1.Column(4) = Me.TextBox4.Text
UserForm1.ListBox1.Column(5) = Me.TextBox5.Text
UserForm1.ListBox1.Column(6) = Me.TextBox6.Text
UserForm1.ListBox1.Column(7) = Me.TextBox7.Text
UserForm1.ListBox1.Column(8) = Me.TextBox8.Text

End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm2.TextBox1.Value = Me.ListBox1.List(0)
UserForm2.TextBox2.Value = Me.ListBox1.List(1)
UserForm2.TextBox3.Value = Me.ListBox1.List(2)
UserForm2.Show

End Sub

Private Sub UserForm_Initialize()
For i = 1 To 3
    Me.ListBox1.AddItem Cells(i, 1).Value
Next
End Sub


Private Sub CommandButton1_Click()
UserForm1.ListBox1.List(0) = Me.TextBox1.Value
UserForm1.ListBox1.List(1) = Me.TextBox2.Value
UserForm1.ListBox1.List(2) = Me.TextBox3.Value

End Sub

enter image description here