【问题标题】:Update userform from another userform, error "Could not set the column property"从另一个用户表单更新用户表单,错误“无法设置列属性”
【发布时间】:2019-08-25 21:25:56
【问题描述】:

我是 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

这是用户表单用于更新“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.

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

【问题讨论】:

标签: excel vba


【解决方案1】:

您需要参考列表框的相关行和列来写入。这是你正在尝试的吗?

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

【讨论】:

  • 您好,很抱歉回复晚了,谢谢您的建议,我试过了,但出现错误“找不到方法或数据成员”
  • 使用 UserForm1.ListBox1 .List(rw, 1) = Time .List(rw, 2) = TextBox1.Text .List(rw, 3) = TextBox2.Text
【解决方案2】:

我自己试过了,现在我会先尝试进入工作表,而不是专注于 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多