Remove Absolute References

datauser

New Member
Joined
Sep 4, 2024
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have a working macro that calculates correctly and starts where I need it to, but I need to remove all the absolute references for the formula so that I can apply the formula down a column. The starting point for my formula will be D5, the ending row number will be the same(5), but ending column will be dynamic.

I would like this part of the code:
formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(" & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ") - COLUMN(" & ws.Cells(targetRow, 4).Address & ") + 1, 2) = 1)," & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ")"
to work like this: =SUMPRODUCT(--(MOD(COLUMN(D5:AH5)-COLUMN(D5)+1,2)=1),D5:AH5) so when I drag the formula down, the cell references will also change
My attempt to define the start of the formula, but have the ending column be dynamic. formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(D5:& LastCol& 5)) - COLUMN(D5) + 1, 2) = 1),D5:& LastCol & 5)"


Sub InsertSumProductFormula3()

Dim ws As Worksheet
Dim lastCol As Long
Dim lastRow As Long
Dim formulaCell As Range
Dim formulaStr As String
Dim targetRow As Long

' Set the worksheet
Set ws = ThisWorkbook.Sheets("Week to Week")

' Specify the target row where you want to insert the formula (e.g., row 5)
targetRow = 5 ' Change to your desired row

' Find the last column with data in the target row
lastCol = ws.Cells(targetRow, ws.Columns.Count).End(xlToLeft).Column

' Find the last row with data in column A (adjust if needed)
lastRow = ws.Cells(ws.Rows.Count, 5).End(xlUp).Row

' Define the cell where the formula will be added (first empty cell in the next row)
Set formulaCell = ws.Cells(targetRow, lastCol + 1) ' Adjust to your desired column

' Construct the dynamic SUMPRODUCT formula
formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(" & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ") - COLUMN(" & ws.Cells(targetRow, 4).Address & ") + 1, 2) = 1)," & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ")"

' Insert the formula
formulaCell.Formula = formulaStr

MsgBox "Formula added in " & formulaCell.Address

End Sub

1730136551107.png
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hello!

I have a working macro that calculates correctly and starts where I need it to, but I need to remove all the absolute references for the formula so that I can apply the formula down a column. The starting point for my formula will be D5, the ending row number will be the same(5), but ending column will be dynamic.

I would like this part of the code:
formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(" & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ") - COLUMN(" & ws.Cells(targetRow, 4).Address & ") + 1, 2) = 1)," & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ")"
to work like this: =SUMPRODUCT(--(MOD(COLUMN(D5:AH5)-COLUMN(D5)+1,2)=1),D5:AH5) so when I drag the formula down, the cell references will also change
My attempt to define the start of the formula, but have the ending column be dynamic. formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(D5:& LastCol& 5)) - COLUMN(D5) + 1, 2) = 1),D5:& LastCol & 5)"


Sub InsertSumProductFormula3()

Dim ws As Worksheet
Dim lastCol As Long
Dim lastRow As Long
Dim formulaCell As Range
Dim formulaStr As String
Dim targetRow As Long

' Set the worksheet
Set ws = ThisWorkbook.Sheets("Week to Week")

' Specify the target row where you want to insert the formula (e.g., row 5)
targetRow = 5 ' Change to your desired row

' Find the last column with data in the target row
lastCol = ws.Cells(targetRow, ws.Columns.Count).End(xlToLeft).Column

' Find the last row with data in column A (adjust if needed)
lastRow = ws.Cells(ws.Rows.Count, 5).End(xlUp).Row

' Define the cell where the formula will be added (first empty cell in the next row)
Set formulaCell = ws.Cells(targetRow, lastCol + 1) ' Adjust to your desired column

' Construct the dynamic SUMPRODUCT formula
formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(" & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ") - COLUMN(" & ws.Cells(targetRow, 4).Address & ") + 1, 2) = 1)," & ws.Cells(targetRow, 4).Address & ":" & ws.Cells(targetRow, lastCol).Address & ")"

' Insert the formula
formulaCell.Formula = formulaStr

MsgBox "Formula added in " & formulaCell.Address

End Sub

View attachment 118642

Does this work.

Note the .Address(False, False) although .Address(True, False) would have locked the column.

VBA Code:
formulaStr = "=SUMPRODUCT(--(MOD(COLUMN(" & ws.Cells(targetRow, 4).Address(False, False) & ":" & ws.Cells(targetRow, _
  lastCol).Address(False, False) & ") - COLUMN(" & ws.Cells(targetRow, 4).Address(False, False) & ") + 1, 2) = 1)," & _
  ws.Cells(targetRow, 4).Address(False, False) & ":" & ws.Cells(targetRow, lastCol).Address(False, False) & ")"
 
Upvote 0
Solution
This works! Thank you so much. This completely stumped me! Working with this VBA would you be able to assist in dragging the formula down the columns?
 
Upvote 0
This works! Thank you so much. This completely stumped me! Working with this VBA would you be able to assist in dragging the formula down the columns?
You are nearly there:

You just need to change the row numbers.

VBA Code:
ws.Range("AI19:AI30").Formula2 = formulaStr
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,096
Members
453,021
Latest member
Justyna P

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