The code below is run from ThisWorkbook. It opens another 4 wb's (Gr1.xlsm, Report1.xls, Report2.xls and Report3.xls), and copies data from wb Report 1-3 into 3 different sheets in wb Gr1.
Vba works, but I'm looking for a better way of writing it. One issue is to avoid as much use of .activate, .select and .selection as possible. Another is to shorten the code if possible. Is it better to use definition on every wb and sh at the top of the code? If so, how to write this?
When copying, I need to use cells.select or something similar 'cause I'm overwriting what ever information that already exists in the sheets I'm pasting data to.
My vba skills is clearly not what it ought to be... Anyone that can help me on how to change this code?
Vba works, but I'm looking for a better way of writing it. One issue is to avoid as much use of .activate, .select and .selection as possible. Another is to shorten the code if possible. Is it better to use definition on every wb and sh at the top of the code? If so, how to write this?
When copying, I need to use cells.select or something similar 'cause I'm overwriting what ever information that already exists in the sheets I'm pasting data to.
My vba skills is clearly not what it ought to be... Anyone that can help me on how to change this code?
Code:
fPath = ThisWorkbook.Path
If Right(fPath, 1) <> "\" Then fPath = fPath
Set wb = Workbooks.Open(ThisWorkbook.Path & "\Gr1.xlsm")
Sheets(«Re1").Visible = True
Sheets(«Re2").Visible = True
Sheets(«Re3").Visible = True
'New reports from wb Report1-3 in to wb Gr1, sh Re1-3
Set wb = Workbooks.Open(fPath & "\R2\" & "Report1" & ".xls")
wb.Sheets(1).Select
Cells.Select
Selection.Copy
wb.Close False
On Error GoTo 0
Windows("Gr1.xlsm").Activate
Sheets(«Re1").Select
Cells.Select
ActiveSheet.Paste
On Error GoTo 0
Set wb = Workbooks.Open(fPath & "\R2\" & "Report2" & ".xls")
wb.Sheets(1).Select
Cells.Select
Selection.Copy
wb.Close False
On Error GoTo 0
Windows("Gr1.xlsm").Activate
Sheets(«Re2").Select
Cells.Select
ActiveSheet.Paste
On Error GoTo 0
Set wb = Workbooks.Open(fPath & "\R2\" & "Report3" & ".xls")
wb.Sheets(1).Select
Cells.Select
Selection.Copy
wb.Close False
On Error GoTo 0
Windows("Gr1.xlsm").Activate
Sheets(«Re3").Select
Cells.Select
ActiveSheet.Paste
On Error GoTo 0