【发布时间】:2015-10-10 02:40:05
【问题描述】:
我正在尝试从其他工作簿复制某些范围的单元格,但出现错误:
'runtime '1004' error
Error defined by application or object
如果我尝试使用“range(cells(i,j), cells(k,h))”sintax 而不是 range(“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
【问题讨论】:
-
欢迎,这个问题和今天刚问的很相似。看here
-
非常相似,很好的捕捉@ScottCraner。
标签: vba excel range copy-paste cells