Hi tech2,
I assumed Report1 and Report2 were Sheets in the same workbook.
If you wish to transfer the Data from Workbook Report1, Sheet Report1, to Workbook Report2, Sheet Report2, you will need to Copy and Paste this revised macro. Deleting previous macro Sub GenerateReport2()
Code error 9 is telling you there is no path to workbook or worksheet so you must get the path right.
The revised macro is actually two macros, The first macro tests to see if workbook Report2 is open, if it is it redirects you to the second macro. If the workbook Report2 is closed, it opens workbook Report2 and then redirects you to the second macro.
This line of code colored red in the macro needs to be changed.
Workbooks.Open Filename:="C:\Users\skinman\Documents\Report2.xlsm"
You need to change to your path, the above is my path. If you don't know the path, Open a new workbook, click on the record macro, then from recent documents select Report2, then stop the macro. Go to the macro just recorded and it will show the correct path. Copy that path, select all the red line and click paste.
You will have to delete the macro Sub GenerateReport2(), it will be replaced by this revised macro of the same name.
Code:
Sub GenerateBook()
On Error GoTo OpenWorkBook:
Dim BookName As String
BookName = "Report2"
Workbooks("Report2").Activate
GenerateReport2
Exit Sub
OpenWorkBook:
If Err.Number = 9 Then
[Color=Red]Workbooks.Open Filename:="C:\Users\skinman\Documents\Report2.xlsm"[/Color]
Resume
End If
GenerateReport2
End Sub
Sub GenerateReport2()
Application.ScreenUpdating = False
Workbooks("Report1").Activate
Sheets("Report1").Select
Range("A1:A1000,B1:B1000,I1:I1000").Select
Selection.Copy
Workbooks("Report2").Activate
Sheets("Report2").Select
If Range("A1") <> ("") Then
Range("$A65536").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Else
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
End If
Workbooks("Report1").Activate
Sheets("Report1").Select
Range("G1:G1000").Select
Selection.Copy
Workbooks("Report2").Activate
Sheets("Report2").Select
If Range("D1") <> ("") Then
Range("$D65536").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
Else
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues
End If
Workbooks("Report1").Activate
Sheets("Report1").Select
Range("A1").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
The macro to run is GenerateBook()
Cheers.