对象Var或With Block未设置VBA

时间:2015-07-31 21:24:29

标签: excel vba excel-vba runtime-error

编写VBA代码以将活动行复制到新工作表。 然后使用文本框条目和列表条目拉出用户表单。 输入后,它将相应地更新单元格并更新日期。

我目前收到运行时错误' 91':对象可变或未设置块变量。如果你可以提供帮助并向我解释,我无法弄清楚这个错误来自哪里我会非常感激!

有两个文件用于调用userform,这是我收到错误的地方。

Sub NewCange()

 ChangeUserForm.Show


End Sub

和用户表单代码

Private Sub UserForm_Initialize()


    Dim i As Integer, M As Integer, Result As Integer
    Dim N As String
    Dim MasterRange As Range, CopyRange As Range, NextRow As Range
    Dim ActiveRow As Long




  'Selects active row copies it and puts in empty row in the copy sheet


     ActiveRow = ActiveCell.Row

            With Worksheets("Master")
                   .Activate
                   .Cells(ActiveRow, 1).Select
                   .Cells(ActiveRow, 1).EntireRow.Copy
            End With

            With Worksheets("Records")
                   .Activate
                   .Cells(ActiveRow, 1).Select
                Rows(ActiveRow).Select
        End With

         ActiveSheet.Paste      
Destination:=Worksheets("Records").Cells(ActiveRow, 1)



'Below here is the inserting new change into excel sheet




    Set MasterRange = Range(ActiveCell, ActiveCell)

     Worksheets("Master").Activate

    LocalListBox.Clear

'List of Locations for assets
With LocalListBox

    .AddItem "TB01 "
    .AddItem "TB02 "
    .AddItem "TB03 "
    .AddItem "TB04 "
    .AddItem "TB05 "
    .AddItem "TB06 "
    .AddItem "TB07 "
    .AddItem "TB07XP "
    .AddItem "TB08 "
    .AddItem "TB09 "
    .AddItem "TB10 "
    .AddItem "TB11 "
    .AddItem "TB12 "
    .AddItem "TB13 "
    .AddItem "TB14 "
    .AddItem "TB15 "
    .AddItem "TB16 "
    .AddItem "TB17 "
    .AddItem "TB17XP "
    .AddItem "TB18 "
    .AddItem "TB19 "
    .AddItem "TB20 "
    .AddItem "TB21 "
    .AddItem "TB23 "
    .AddItem "TB24 "
    .AddItem "TB25 "
    .AddItem "TB26 "
    .AddItem "TB27 "
    .AddItem "TB27XP "
    .AddItem "TB28 "
    .AddItem "TB29 "
    .AddItem "TB30 "
    .AddItem "TB31 "
    .AddItem "TB32 "
    .AddItem "CAB3 "
    .AddItem "CAB4 "
    .AddItem "CAB5 "
    .AddItem "CAB6 "
    .AddItem "CAB7 "
    .AddItem "CAB8 "
    .AddItem "CAB9 "
    .AddItem "CAB10 "
    .AddItem "CAB12 "
    .AddItem "CAB16 "
    .AddItem "CAB17 "
    .AddItem "CAB18 "
    .AddItem "CAB19 "
End With


   Set NameRange = ActiveCell

MasterRange = LocalListBox.Value
MasterRange.Offset(0, 2) = NameTextBox.Value
MasterRange.Offset(0, 1) = Date

Unload Me


End Sub

2 个答案:

答案 0 :(得分:1)

  

运行时错误'91':对象变量或未设置块变量。

您正在收到该错误,因为您正在尝试卸载刚刚初始化的内容。

所以基本上你不能这样做

Private Sub UserForm_Initialize()
    Unload Me
End Sub

删除Unload Me并将其按下按钮。这样,您还可以让用户从列表框LocalListBox中选择一个值:)

实际上,在按钮单击

中移动此代码块
Private Sub CommandButton1_Click()
    MasterRange = LocalListBox.Value
    MasterRange.Offset(0, 2) = NameTextBox.Value
    MasterRange.Offset(0, 1) = Date

    Unload Me
End Sub

并在用户表单的顶部声明Dim MasterRange As Range,而不是UserForm_Initialize()

答案 1 :(得分:0)

您似乎错过了With声明中的句号:

With Worksheets("Records")
               .Activate
               .Cells(ActiveRow, 1).Select
            Rows(ActiveRow).Select
    End With

应该是

   With Worksheets("Records")
           .Activate
           .Cells(ActiveRow, 1).Select
        .Rows(ActiveRow).Select
End With

另外,要准确找到错误的位置,请使用F8到"浏览"宏。此外,强烈建议在VBA中使用avoid using .Select