Excel 从另一个userform更新userform,错误“0”;无法设置列属性";

Excel 从另一个userform更新userform,错误“0”;无法设置列属性";,excel,vba,Excel,Vba,我是VBA新手 我有一个excel工作表,它正在使用另一个userform更新userform 我将调用需要更新的userform作为“userform1” Userform用作“userform2”的更新表单 下面是更新需要更新的userform的代码 Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) UserForm2.TextBox1.Text = Me.ListBox1.Column(1)

我是VBA新手

我有一个excel工作表,它正在使用另一个userform更新userform

我将调用需要更新的userform作为“userform1”
Userform用作“userform2”的更新表单

下面是更新需要更新的userform的代码

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    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.Show
End Sub
以下是userform用于更新“userform1”的代码

当我填充userform2来更新userform1时,我有这个错误

Run-time error '70':

Could not set the column property. Permission denied.
有人能告诉我哪里做错了吗?
非常感谢您的帮助。

您需要参考列表框的相关行和列来写入它。这就是你想要的吗

Private Sub CommandButton1_Click()
    Dim rw As Long

    '~~> Get the row which was selected
    rw = UserForm1.ListBox1.ListIndex
    If rw = - 1 The Exit Sub

    With UserForm1.ListBox1
        .List(rw, 1) = Time
        .List(rw, 2) = TextBox1.Text
        .List(rw, 3) = TextBox2.Text
        .List(rw, 4) = TextBox3.Text
        .List(rw, 5) = TextBox4.Text
        .List(rw, 6) = TextBox5.Text
        .List(rw, 7) = TextBox6.Text
    End With

    TextBox1.Value = "": TextBox2.Value = "": TextBox3.Value = ""
    TextBox4.Value = "": TextBox5.Value = "": TextBox6.Value = ""
End Sub

您需要参考列表框的相关行和列来写入它。这就是你想要的吗

Private Sub CommandButton1_Click()
    Dim rw As Long

    '~~> Get the row which was selected
    rw = UserForm1.ListBox1.ListIndex
    If rw = - 1 The Exit Sub

    With UserForm1.ListBox1
        .List(rw, 1) = Time
        .List(rw, 2) = TextBox1.Text
        .List(rw, 3) = TextBox2.Text
        .List(rw, 4) = TextBox3.Text
        .List(rw, 5) = TextBox4.Text
        .List(rw, 6) = TextBox5.Text
        .List(rw, 7) = TextBox6.Text
    End With

    TextBox1.Value = "": TextBox2.Value = "": TextBox3.Value = ""
    TextBox4.Value = "": TextBox5.Value = "": TextBox6.Value = ""
End Sub

我自己也试过了,现在我将首先尝试进入工作表,而不是关注userform2,我需要了解如何插入表行而不遵循上面的格式

Private Sub CommandButton2_Click()
Dim Kolom As Long
Dim Lembar As Worksheet
Set Lembar = Worksheets("Sheet1")
Kolom = Lembar.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

Dim time As Date
time = Format(Now, "dd-mm-yyyy hh:mm")



If Range("a2") <> "" Then

'Lembar.Range("a2").Select
Range("a2").EntireRow.Insert shift:=xlDown

End If

If Range("a2") = "" Then

Lembar.Range("A2").Value = time
Lembar.Range("B2").Value = Me.TextBox1
Lembar.Range("C2").Value = Me.TextBox2
Lembar.Range("D2").Value = Me.TextBox3
Lembar.Range("E2").Value = Me.TextBox4
Lembar.Range("F2").Value = Me.TextBox5
Lembar.Range("G2").Value = Me.TextBox6

End If
Private子命令按钮2\u单击()
昏暗的科洛姆一样长
将工具栏设置为工作表
设置Lembar=工作表(“表1”)
Kolom=Lembar.Cells(Rows.Count,1)_
.结束(xlUp).偏移量(1,0).行
暗时间为日期
时间=格式(现在为“dd-mm-yyy-hh:mm”)
如果范围为“a2”,则
'Lembar.Range(“a2”)。选择
范围(“a2”).EntireRow.Insert shift:=xlDown
如果结束
如果范围(“a2”)=”则
Lembar.范围(“A2”).值=时间
Lembar.Range(“B2”).Value=Me.TextBox1
Lembar.Range(“C2”).Value=Me.TextBox2
Lembar.Range(“D2”).Value=Me.TextBox3
Lembar.Range(“E2”).Value=Me.TextBox4
Lembar.Range(“F2”).Value=Me.TextBox5
Lembar.Range(“G2”).Value=Me.TextBox6
如果结束

它成功地插入了新行和值,但上面的格式如下。我不希望发生这种事情,在那之后,我们可以把我的主要问题作为下一步。

我自己也试过了,现在我将尝试先进入工作表,而不是关注userform2,我需要了解如何插入表行而不遵循上面的格式

Private Sub CommandButton2_Click()
Dim Kolom As Long
Dim Lembar As Worksheet
Set Lembar = Worksheets("Sheet1")
Kolom = Lembar.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

Dim time As Date
time = Format(Now, "dd-mm-yyyy hh:mm")



If Range("a2") <> "" Then

'Lembar.Range("a2").Select
Range("a2").EntireRow.Insert shift:=xlDown

End If

If Range("a2") = "" Then

Lembar.Range("A2").Value = time
Lembar.Range("B2").Value = Me.TextBox1
Lembar.Range("C2").Value = Me.TextBox2
Lembar.Range("D2").Value = Me.TextBox3
Lembar.Range("E2").Value = Me.TextBox4
Lembar.Range("F2").Value = Me.TextBox5
Lembar.Range("G2").Value = Me.TextBox6

End If
Private子命令按钮2\u单击()
昏暗的科洛姆一样长
将工具栏设置为工作表
设置Lembar=工作表(“表1”)
Kolom=Lembar.Cells(Rows.Count,1)_
.结束(xlUp).偏移量(1,0).行
暗时间为日期
时间=格式(现在为“dd-mm-yyy-hh:mm”)
如果范围为“a2”,则
'Lembar.Range(“a2”)。选择
范围(“a2”).EntireRow.Insert shift:=xlDown
如果结束
如果范围(“a2”)=”则
Lembar.范围(“A2”).值=时间
Lembar.Range(“B2”).Value=Me.TextBox1
Lembar.Range(“C2”).Value=Me.TextBox2
Lembar.Range(“D2”).Value=Me.TextBox3
Lembar.Range(“E2”).Value=Me.TextBox4
Lembar.Range(“F2”).Value=Me.TextBox5
Lembar.Range(“G2”).Value=Me.TextBox6
如果结束

它成功地插入了新行和值,但上面的格式如下。我不想发生这种事情,在那之后,我们可以把我的主要问题带到下一步。

看看@user2648008:对不起。刚刚看到你的链接。我在测试我写的东西……看看@user2648008:对不起。刚刚看到你的链接。我正在测试我写的内容…嗨,很抱歉回复太晚,谢谢你的建议,我已经尝试过了,但是UserForm1.ListBox1.List(rw,1)=Time.List(rw,2)=TextBox1.Text.List(rw,3)=TextBox2.TextHi上出现错误“找不到方法或数据成员”,很抱歉回复太晚,谢谢你的建议,我已经尝试过了,但在UserForm1.ListBox1.List(rw,1)=Time.List(rw,2)=TextBox1.Text.List(rw,3)=TextBox2.Text上出现错误“找不到方法或数据成员”