对象变量/块变量未设置调试

时间:2017-06-13 08:06:59

标签: excel vba excel-vba

我不知道出了什么问题,但每当我运行宏时,它都会给我一个运行时错误91对象变量或者没有设置块变量。这个的总结是我想只复制名称(Sakinah),A列:来自只读工作簿(主)的Q并粘贴到Sakinah列A:Q.现在,复制部分工作正常,它只是粘贴部分。此代码输入Sakinah,我将其定义为Thisworkbook我不确定出了什么问题。请帮忙。

以下是我的代码:我突出显示了发生错误的部分:

Sub Update_ReadOnly_Click()

Dim copyStartCellSakinah As Long

Dim excelFilePath
Dim masterWorkBook As Workbook

Dim readLastCell As Long
Dim manager As String
Dim A
Dim B
Dim C
Dim d
Dim E
Dim F
Dim G
Dim H
Dim I
Dim J
Dim K
Dim L
Dim M
Dim N
Dim O
Dim P
Dim Q

Dim sheet As String


Dim sakinahWorkBookPath As String
Dim masterWorkBookPath As String
Dim Sakinahworkbook As ThisWorkbook

excelFilePath = Application.ActiveWorkbook.Path

masterWorkBookPath = excelFilePath & "\Master.xlsm"

Set masterWorkBook = Workbooks.Open(masterWorkBookPath, ReadOnly = True)

'get copy row
copyStartCellSakinah = 4

readLastCell = masterWorkBook.Sheets("Master").Cells(Rows.Count, 

1).End(xlUp).Row + 1

    For x = 4 To readLastCell
    cell = "C" & x
    manager = masterWorkBook.Worksheets("Master").Range(cell).Value

        If UCase(manager) = "SAKINAH" Then
            sheet = "Sakinah"


            cell = "A" & x
            A = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "B" & x
            B = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "C" & x
            C = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "D" & x
            d = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "E" & x
            E = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "F" & x
            F = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "G" & x
            G = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "H" & x
            H = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "I" & x
            I = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "J" & x
            J = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "K" & x
            K = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "L" & x
            L = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "M" & x
            M = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "N" & x
            N = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "O" & x
            O = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "P" & x
            P = masterWorkBook.Worksheets("Master").Range(cell).Value
            cell = "Q" & x
            Q = masterWorkBook.Worksheets("Master").Range(cell).Value

'直到这一点它没关系,但是当我进入Sakinahworkbook.worksheets(表)时它给了我一个错误。

 'copy


 cell = "A" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = A
         cell = "B" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = B
         cell = "C" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = C
         cell = "D" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = d
         cell = "E" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = E
         cell = "F" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = F
        cell = "G" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = G
        cell = "H" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = H
        cell = "I" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = I
        cell = "J" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = J
        cell = "K" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = K
        cell = "L" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = L
        cell = "M" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = M
        cell = "N" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = N
        cell = "O" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = O
        cell = "P" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = P
        cell = "Q" & copyStartCellSakinah
        Sakinahworkbook.Worksheets(sheet).Range(cell).Value = Q


        copyStartCellSakinah = copyStartCellSakinah + 1

    End If
    Next x

' CLOSE THE SOURCE FILE.
    masterWorkBook.Close True             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set masterWorkBook = Nothing
Application.EnableEvents = True



'Me.Visible = True
MsgBox "Updated Successfully"
End Sub

2 个答案:

答案 0 :(得分:0)

您应将ext_timed_batch声明为Sakinahworkbook,然后将Workbook声明为Set

ThisWorkbook

我非常惊讶Dim Sakinahworkbook As Workbook Set Sakinahworkbook = ThisWorkbook 甚至在语法上被允许。

答案 1 :(得分:0)

要摆脱这个错误,你必须拆分声明和赋值:

Dim Sakinahworkbook As ThisWorkbook

分为:

Dim Sakinahworkbook As Workbook
Set Sakinahworkbook = ThisWorkbook

此外,我建议通过在一个循环中执行复制操作来简化此代码,而不是为每个列引入变量:

For x = 1 to  readLastCell
    For i = 1 to 17
        Sakinahworkbook.Worksheets(sheet).Range(x,i).Value = masterWorkBook.Worksheets("Master").Range(x,i).Value
    Next i
Next x