vba从其他工作簿导入单元格错误

时间:2015-10-09 18:29:32

标签: vba excel-vba range copy-paste cells

我正在尝试从其他工作簿复制某些范围的单元格,但是我收到错误:

'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

0 个答案:

没有答案