VBA Excel:子例程工作,但如果调用,它不再有效

您好那里遇到一个CALL问题

我创build了一个名为Daily_Proto_1的子例程,它将用户表单中的数据复制到工作表中:这是代码:

 Public Sub Daily_Proto_1() Dim LastRow As Long, ws As Worksheet Set ws = Sheets("DailyRep") LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row 'Adds the TextBox1 into Col A & Last Blank Row 'Adds the TextBox2 into Col B & Last Blank Row 'Adds the ComboBox1 into Col C & Last Blank Row 'Ect. ws.Range("A" & LastRow).Value = TextBox1.Text ws.Range("B" & LastRow).Value = TextBox2.Text ws.Range("C" & LastRow).Value = ComboBox1.Text ws.Range("D" & LastRow).Value = TextBox3.Text ws.Range("E" & LastRow).Value = ComboBox2.Text ws.Range("F" & LastRow).Value = ComboBox3.Text End Sub 

现在在我的用户表单中,我有一个提交button,应按照以下规定调用此例程:

 Private Sub CommandButton1_Click() Call Module1.Daily_Proto_1 End Sub 

这样做的原因是我可以将其包含在另一个button中,但会有一些额外的说明。 当我在Private Sub CommandButton1_Click()下运行代码时,它运行得很完美,但是当我把它移动到Public Procedure时,它给了我下面的错误: 错误信息

当我点击debugging时,需要我到第一行 VBA代码

任何人都可以告诉我,我在这里做错了

在TextBox1之前添加您的用户窗体的名称。 例如

  ws.Range("A" & LastRow).Value = UserForm1.TextBox1.Text 

或者宁可使用

 with UserForm1 end with 

点beforre控制名称。

我想你可能需要在textbox1.text之前规定你的用户表单的名字。

 ws.Range("A" & LastRow).Value = UserForm1.textbox1.text 

(用用户表单的名称replaceUserform1)