0

Every year I have a new annual Excel file that has the same structure as the previous year. In the previous year I have defined rows and columns that I have to read from all the sheets of that Excel file, and I return the calculation result to that same file.

At the new year I need to repeat the whole procedure. Is there a way to move links and formulas from the previous year to the next year without manually rebinding the data?

This is recorded Macro:

Sub NewYearData()

' NewYearData Makronaredba
    Windows("PreviousYear.xlsm").Activate
    Sheets("1.A.1.a.ii Liquid Fuels").Select
    Range("C16:AH18").Select
    Selection.Copy
    Application.WindowState = xlNormal
    Windows("CurrentYear2.xlsx").Activate
    Range("C16").Select
    ActiveSheet.Paste Link:=True
    ActiveSheet.Paste
    Windows("PreviousYear.xlsm").Activate
    Sheets("1.A.1.a.ii Gaseous Fuels Gaseo").Select
    Range("C16:AH18").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("CurrentYear2.xlsx").Activate
    Sheets("1.A.1.a.ii Gaseous Fuels Gaseo").Select
    Range("C16:AH18").Select
    ActiveSheet.Paste
End Sub

How to make it to for all sheets?

  1. For all sheets with same name in both file
  2. find cells where reference is
  3. copy reference from source file to destination file at same location

1 Answer 1

0

This assumes that we have the same sheets (with the same names) in both workbooks. We define some objects and use a loop:

Sub NewYearData()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim r1 As Range, r2 As Range
    Dim addy As String, sh As Worksheet
    Dim shName As String
    
    Set wb1 = Workbooks("PreviousYear.xlsm")
    Set wb2 = Workbooks("CurrentYear2.xlsx")
    addy = "C16:AH18"
    
    For Each sh In wb1.Sheets
        shName = sh.Name
        Set r1 = sh.Range(addy)
        Set r2 = wb2.Sheets(shName).Range(addy)
        r1.Copy r2
    Next sh
    
End Sub
1
  • Thanks! What if references are not at the same place (addy = "C16:AH18")? How can i find references because i must copy all references! Commented Jan 21, 2021 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .