我不知道什麼是錯誤的,但每當我運行宏,它給我一個運行時錯誤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
可能是一個小整理者調查使用「響」 e'而不是你的代碼,所以'range(「a」&x&「:」q「&x).offset('很多整理者,你的a到q都沒有被有效地定義......甚至沿着這些行'ws2.range()。value = ws1.range()。value' –
@Nathan_Sav幫助使它成爲一個更清潔的版本?這裏有一個完整的新手 – Carmen