1
我試圖複製其他工作簿中的細胞的一定範圍內,但我得到的錯誤:VBA導入細胞錯誤
'runtime '1004' error
Error defined by application or object
,如果我嘗試使用「範圍(單元格(I,J ),單元格(k,h))「sintax而不是範圍(」A1:Z1「)。即,在下面的代碼中,行「PASTE 1」產生一個錯誤,而行「PASTE 2」順利運行(顯然,我不想使用第二個,因爲我需要在不同的範圍內運行一個循環)。
Sub Importa()
Dim directory As String
Dim fileName As String
Dim wbfrom As Workbook
Dim wbto As Workbook
Application.ScreenUpdating = False
Application.DisplayAlerts = False
directory = "mydirectory"
fileName = Dir(directory & "*.xl??") 'find the first *.xl?? file; ' wildcards: multiple character (*) single character (?)
Set wbto = ThisWorkbook
Set wbfrom = Workbooks.Open(directory & fileName, False, True)
' copy some cells
wbfrom.Sheets(1).Range(Cells(9, 6), Cells(15, 6)).Copy
'PASTE 1
wbto.Sheets(1).Range(Cells(9, 1), Cells(15, 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'PASTE 2
'wbto.Sheets(1).Range("A1:A8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
wbfrom.Close SaveChanges:=False
'Turn on screen updating and displaying alerts again
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
歡迎,這個問題非常類似於今天剛剛提出的問題。看[這裏](http://stackoverflow.com/questions/33041178/vba-for-each-worksheet-in-workbook) –
非常相似,很好的抓住@ScottCraner。 –