Forum Discussion

Susan1085's avatar
Susan1085
Copper Contributor
Jun 12, 2025
Solved

Macro Formula Relative Referencing

Hi there

 

I am trying to create a macro to use at the end of every month to convert a report into a format the client requires.

The amount of rows in the report will differ every month so using relative referencing.  

I cannot figure out how to get the formulas in Cells G2 & H2 (which I have added while creating the macro) to reproduce regardless of the amount of rows in the report? They always stop at row 14.

Thanks for any help. Sheet attached.

Quit new to this.

Microsoft® Excel® for Microsoft 365 MSO (Version 2505 Build 16.0.18827.20102) 64-bit

 PC Windows 11 Business

  • That worked. Here is a working macro:

    Sub TransformData()
        Dim LastRow As Long
        Application.ScreenUpdating = False
        ' Delete Customer Code and Customer Name
        Range("A1:B1").EntireColumn.Delete
        ' Move Date to column A
        Range("I1").EntireColumn.Cut
        Range("A1").EntireColumn.Insert
        ' Move Transaction Code to column B
        Range("O1").EntireColumn.Cut
        Range("B1").EntireColumn.Insert
        ' Move Docket number to column C
        Range("I1").EntireColumn.Cut
        Range("C1").EntireColumn.Insert
        ' Delete Customer Address1 to Driver Code
        Range("F1:J1").EntireColumn.Delete
        ' Delete Paid Value to Location Description
        Range("G1:K1").EntireColumn.Delete
        ' Create new headers
        Range("G1").Value = "Percy Mills"
        Range("H1").Value = "Other"
        ' Create Formulas in columns G and H
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        Range("G2:G" & LastRow).Formula = "=IF(ISNUMBER(SEARCH($G$1,E2)),F2,"""")"
        Range("H2:H" & LastRow).Formula = "=IF(ISNUMBER(SEARCH($G$1,E2)),"""",F2)"
        ' Totals
        Range("A" & LastRow + 1).Value = "Totals"
        Range("F" & LastRow + 1).Resize(1, 3).Formula = "=SUM(F2:F" & LastRow & ")"
        ' Number format for columns F to H
        Range("F2:H" & LastRow + 1).NumberFormat = "#,##0.00"
        ' Column Widths
        Range("A1").CurrentRegion.EntireColumn.AutoFit
        Application.ScreenUpdating = True
    End Sub

     

10 Replies

    • HansVogelaar's avatar
      HansVogelaar
      MVP

      Thank you. Unfortunately, that does not provide a clue.

      Could you attach a small sample workbook demonstrating the problem (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar?

      And indicate which cell or cells you select before running the macro.

      • Susan1085's avatar
        Susan1085
        Copper Contributor

        I sent a reply with a link to a small worksheet in google drive.

        I do not know what is going on. I have posted with spreadsheet attached 3 times and it has never shown up.

        It seems to be taking over 24 hours for my replies to appear.....if they appear at all!

  • Susan1085's avatar
    Susan1085
    Copper Contributor

    Hi Hans

     

    Apologies, I though it attached it the first time.

Resources