I am trying to copy some range of cells from other workbooks, but I get the error:
'runtime '1004' error
Error defined by application or object
if I try to use the "range(cells(i,j), cells(k,h))" sintax instead of the range("A1:Z1"). I.e., In the following code the line "PASTE 1" produces an error, while the line "PASTE 2" runs smoothly (obviously I don't want to use the second one because I need to run a loop over different ranges).
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