2017-06-13 72 views
0

我不知道什麼是錯誤的,但每當我運行宏,它給我一個運行時錯誤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 
+0

可能是一個小整理者調查使用「響」 e'而不是你的代碼,所以'range(「a」&x&「:」q「&x).offset('很多整理者,你的a到q都沒有被有效地定義......甚至沿着這些行'ws2.range()。value = ws1.range()。value' –

+0

@Nathan_Sav幫助使它成爲一個更清潔的版本?這裏有一個完整的新手 – Carmen

回答

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 
+0

我嘗試過使用建議的解決方案將此工作簿定義爲此工作簿,但它仍然無法正常工作。至於第二行代碼,是否會運行我的代碼?你能幫助更清潔的版本?真的真的是新手。謝謝!! – Carmen

+0

您確定自己的工作薄中有「Sakinah」表嗎? – smartobelix

+0

是的,該工作表在Sakinah工作簿中被命名爲Sakinah。 – Carmen

0

你應該聲明SakinahworkbookWorkbook,然後SetThisWorkbook

Dim Sakinahworkbook As Workbook 
Set Sakinahworkbook = ThisWorkbook 

我非常驚訝的是As ThisWorkbook甚至允許語法。

+0

謝謝你的建議。我試過了,但仍然無法正常工作。任何建議? – Carmen

相關問題