从另一个用户窗体更新用户窗体,错误“无法设置列属性”

时间:2019-04-04 07:50:38

标签: excel vba

我是VBA的新手,

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

我将需要更新的用户表单称为“ userform1”
用户表单用作“ userform2”的更新表单

这是用于更新需要更新的用户表单的代码

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”的代码

Private Sub CommandButton1_Click()
        UserForm1.ListBox1.Column(1) = time
        UserForm1.ListBox1.Column(2) = Me.TextBox1.Text
        UserForm1.ListBox1.Column(3) = Me.TextBox2.Text
        UserForm1.ListBox1.Column(4) = Me.TextBox3.Text
        UserForm1.ListBox1.Column(5) = Me.TextBox4.Text
        UserForm1.ListBox1.Column(6) = Me.TextBox5.Text
        UserForm1.ListBox1.Column(7) = Me.TextBox6.Text
        Me.TextBox1.Value = ""
        Me.TextBox2.Value = ""
        Me.TextBox3.Value = ""
        Me.TextBox4.Value = ""
        Me.TextBox5.Value = ""
        Me.TextBox6.Value = ""
End Sub

当我填写userform2以更新userform1时,出现此错误

Run-time error '70':

Could not set the column property. Permission denied.

有人可以告诉我我在哪里做错了吗? 任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:0)

您需要参考列表框的相关行和列才能对其进行写入。这是您要尝试的吗?

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

答案 1 :(得分:0)

我已经尝试过自己,现在我将首先尝试入门,而不是专注于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

它成功插入了新行和值,但它遵循以下格式。我不希望这种事情发生,在那之后我们可以将我的主要问题用于下一步。