Macro to copy data from source workbook sheets to historic workbook sheets

Bobson

New Member
Joined
Dec 23, 2015
Messages
9
My aim is to write a macro which will copy monthly data from the source, which is a workbook with data in multiple sheets, into my historic workbook which has the same multiple sheets but obviously the previous historic data as well.

So far I have managed to pull together some code which allow me to select the file, copy the range which will paste in the blank cell below the current data.

Code:
Sub copy()

    Dim wbSource As Workbook
    Dim wsDest As Worksheet
    Dim SourceFileName As String
    
    SourceFileName = Application.GetOpenFilename(FileFilter:="Excel Files (*xls),*xls", Title:="Please select a file")
    
    
    Set wsDest = ThisWorkbook.Worksheets("Sheet1")
    
    Application.ScreenUpdating = False
 
    Set wbSource = Workbooks.Open(Filename:=SourceFileName, ReadOnly:=True)
    
    wbSource.Sheets("Sheet1").Range("A1:C6").copy
    wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    wsDest.Range("A1").PasteSpecial xlPasteFormats
    
    wbSource.Close False
    
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
  
End Sub


What I need is to write code so this happens to all sheets at the same time. Sheet1 source workbook to Sheet1 in the historic workbook and so on... Sheet2 to Sheet2...... up to Sheet9 to Sheet9.

I am getting confused with how to Dimension the different sheets and would appreciate some help.

Do I need to dimension each sheet and repeat the code for every copy paste i want to do?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
So I have got this far but my loop just copies the final sheet into all the sheets.

Code:
Sub copy()

    Dim wbSource As Workbook, wsDest As Workbook
    Dim ws As Worksheet, rng As Range
    Dim SourceFileName As String
    
    Application.ScreenUpdating = False
        
    SourceFileName = Application.GetOpenFilename(FileFilter:="Excel Files (*xls),*xls", Title:="Please select a file")
    Set wsDest = ThisWorkbook
    
 
 
Set wbSource = Workbooks.Open(Filename:=SourceFileName, ReadOnly:=True)
    


For Each ws In wbSource.Sheets
Range("A1:C6").copy
        
        
wsDest.Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        
Next ws
    
    CutCopyMode = False
    wbSource.Close SaveChanges:=False
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
Code:
Sub copy()

    Dim wbSource As Workbook, wsDest As Workbook
    Dim ws As Worksheet, rng As Range
    Dim SourceFileName As String
    Dim LastCol As Long
    




'I need to find a way of dealing with a variable column length..






    Application.ScreenUpdating = False
        
    SourceFileName = Application.GetOpenFilename(FileFilter:="Excel Files (*xls),*xls", Title:="Please select a file")
    Set wsDest = ThisWorkbook
  
Set wbSource = Workbooks.Open(Filename:=SourceFileName, ReadOnly:=True)
    


For Each ws In wbSource.Sheets




ws.Range(1, Cells(1, Columns.Count).End(xlToLeft)).copy
        
        
wsDest.Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        
Next ws
    
    CutCopyMode = False
    wbSource.Close SaveChanges:=False
    Application.ScreenUpdating = True
 
End Sub

I don't understand why this wont work
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top