Hello All ~
I am writing a macro to prepare a tab for the next year's data. I have been searching this forum and have found many answers to issues a long the way. I have searched and tried many things but cant get this last part to work correctly.
Right now all the coding that is active (shown below) is working. The only part I cant figure out how to change is to get the SUM formula created in the new YTD column to have the the first column be absolute and the rest of the formula to be relative so I can add additional coding to copy the formula down. example. when i currently run the macro, it creates a new YTD column in column AK and I want a formula to go into AK5 that is basically =SUM($AI5:AJ5) and rignt now the formula that gets entered is SUM($AI$5:$AJ$5) (of course the column reference would change each year)
I have tried the ConvertFormula coding (inactive code that is in bold, red below) but there is something that I dont have right as i keep getting "object required" error messages...I have tried setting up a second object and such but to no avail.
Any help would be appreciated.
Sub MatFlowNewYear()
'
' Prepares the Mat Flow tab for the new year's data
'
'Find the last column with data in row 4 (the row showing the month) and delete the thin column that is 2 columns to the left
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -2).Delete
'Insert three new columns to left of "current month's" data column
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Insert
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -1).Insert
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -2).Insert
'Change the column width of the two left columns created
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -2).Select
ActiveCell.ColumnWidth = 2
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -3).Select
ActiveCell.ColumnWidth = 2
'change the color of the far left empty column just created
Range("IV4").End(xlToLeft).Select
ActiveCell.Offset(-3, -3).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Interior.color = RGB(0, 0, 0)
'Label the column to the left of "current month's" data with a YTD and put the new year in the cell above that
Range("IV4").End(xlToLeft).Select
ActiveCell.Offset(0, -1).Select
ActiveCell.FormulaR1C1 = "YTD"
ActiveCell.Offset(-1, 0).Select
ActiveCell.FormulaR1C1 = "=YEAR(R[1]C[1])"
'Enter SUM formula in new YTD column
Dim lCol As Variant
Range("IV5").End(xlToLeft).Select
ActiveCell.Offset(0, -2).Select
lCol = ActiveCell.Address
'lCol.Formula = Application.ConvertFormula(Formula:=lCol.Formula, fromreferencestyle _
:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlRelRowRelColumn)
Dim fCol As Variant
Range("IV5").End(xlToLeft).Select
ActiveCell.Offset(0, -3).Select
fCol = ActiveCell.Address
'fCol.Formula = Application.ConvertFormula(Formula:=fCol.Formula, fromreferencestyle _
:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlRelRowAbsColumn)
Range("IV5").End(xlToLeft).Select
ActiveCell.Offset(0, -1).Select
ActiveCell.Formula = "=SUM(" & fCol & ":" & lCol & ")"
'ActiveCell.FormulaR1C1 = "=SUM(INDIRECT(RC-2:RC[-1]))"
'Set fCol = Nothing
'Set lCol = Nothing
MsgBox "Completed! ", vbInformation, "Mat Flow Tab"
End Sub
I am writing a macro to prepare a tab for the next year's data. I have been searching this forum and have found many answers to issues a long the way. I have searched and tried many things but cant get this last part to work correctly.
Right now all the coding that is active (shown below) is working. The only part I cant figure out how to change is to get the SUM formula created in the new YTD column to have the the first column be absolute and the rest of the formula to be relative so I can add additional coding to copy the formula down. example. when i currently run the macro, it creates a new YTD column in column AK and I want a formula to go into AK5 that is basically =SUM($AI5:AJ5) and rignt now the formula that gets entered is SUM($AI$5:$AJ$5) (of course the column reference would change each year)
I have tried the ConvertFormula coding (inactive code that is in bold, red below) but there is something that I dont have right as i keep getting "object required" error messages...I have tried setting up a second object and such but to no avail.
Any help would be appreciated.
Sub MatFlowNewYear()
'
' Prepares the Mat Flow tab for the new year's data
'
'Find the last column with data in row 4 (the row showing the month) and delete the thin column that is 2 columns to the left
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -2).Delete
'Insert three new columns to left of "current month's" data column
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Insert
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -1).Insert
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -2).Insert
'Change the column width of the two left columns created
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -2).Select
ActiveCell.ColumnWidth = 2
Range("IV4").End(xlToLeft).Select
ActiveCell.EntireColumn.Offset(0, -3).Select
ActiveCell.ColumnWidth = 2
'change the color of the far left empty column just created
Range("IV4").End(xlToLeft).Select
ActiveCell.Offset(-3, -3).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Interior.color = RGB(0, 0, 0)
'Label the column to the left of "current month's" data with a YTD and put the new year in the cell above that
Range("IV4").End(xlToLeft).Select
ActiveCell.Offset(0, -1).Select
ActiveCell.FormulaR1C1 = "YTD"
ActiveCell.Offset(-1, 0).Select
ActiveCell.FormulaR1C1 = "=YEAR(R[1]C[1])"
'Enter SUM formula in new YTD column
Dim lCol As Variant
Range("IV5").End(xlToLeft).Select
ActiveCell.Offset(0, -2).Select
lCol = ActiveCell.Address
'lCol.Formula = Application.ConvertFormula(Formula:=lCol.Formula, fromreferencestyle _
:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlRelRowRelColumn)
Dim fCol As Variant
Range("IV5").End(xlToLeft).Select
ActiveCell.Offset(0, -3).Select
fCol = ActiveCell.Address
'fCol.Formula = Application.ConvertFormula(Formula:=fCol.Formula, fromreferencestyle _
:=xlA1, ToReferenceStyle:=xlA1, ToAbsolute:=xlRelRowAbsColumn)
Range("IV5").End(xlToLeft).Select
ActiveCell.Offset(0, -1).Select
ActiveCell.Formula = "=SUM(" & fCol & ":" & lCol & ")"
'ActiveCell.FormulaR1C1 = "=SUM(INDIRECT(RC-2:RC[-1]))"
'Set fCol = Nothing
'Set lCol = Nothing
MsgBox "Completed! ", vbInformation, "Mat Flow Tab"
End Sub