asked on # Excel VBA to copy the first formula found to all of the rows in that column

I am trying to create a macro that will find the column that has "Total Fee" as the column heading in row 5. Go down the column to the first formula (other rows are blank) and copy that formula up and down the other cells on that column (can overwrite all data) to the last cell in that column (there are blank cells).

After that turn all formulas in that column to values.

Thanks, Eric

After that turn all formulas in that column to values.

Thanks, Eric

Microsoft Excel

Another question

Should the formula remain "as is" during the copy or do you need the appropriate values to move with the formula ie the equivalent of dragging the formula up or down

Should the formula remain "as is" during the copy or do you need the appropriate values to move with the formula ie the equivalent of dragging the formula up or down

To get started here is a macro that assumes that the column with the formula has a value in the last row

The code to copy the formula contains both an increment and exact copy sections. Just remove the single quotes from the start of the section you want (Only the ones at the start of the line)

The code to copy the formula contains both an increment and exact copy sections. Just remove the single quotes from the start of the section you want (Only the ones at the start of the line)

```
Sub macro1()
Const START_ROW As Integer = 6
Const HEADER_ROW As Integer = 5
Const HEADER_NAME As String = "Total Fee"
Dim i As Integer, lastRow As Integer, col As Integer, fRow As Integer
Dim fmla As String
' Get column number
i = 1
While (col = 0)
If (Cells(HEADER_ROW, i).Value = HEADER_NAME) Then
col = i
Else
i = i + 1
End If
Wend
' Set last row
lastRow = Cells(Rows.Count, col).End(xlUp).Row
'Get Formula
i = START_ROW
While (i <= lastRow And fmla = "")
If (Cells(i, col).HasFormula) Then
fmla = Cells(i, col).formula
fRow = i
End If
i = i + 1
Wend
If (fmla = "") Then
MsgBox "No formula found"
Exit Sub
End If
' ' Copy formula into column (exact)
' For i = START_ROW To lastRow
' Cells(i, col).formula = fmla
' Next i
'
' Copy formula into column (increment)
' Cells(fRow, col).AutoFill Destination:=Range(Cells(START_ROW, col), Cells(fRow, col)), Type:=xlFillDefault
' Cells(fRow, col).AutoFill Destination:=Range(Cells(fRow, col), Cells(lastRow, col)), Type:=xlFillDefault
' Remove formulas
Range(Cells(START_ROW, col), Cells(lastRow, col)).Copy
Range(Cells(START_ROW, col), Cells(lastRow, col)).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
```

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!

James Murphy

Log in or sign up to see answer

Become an EE member today7-DAY FREE TRIAL

Members can start a 7-Day Free trial then enjoy unlimited access to the platform

or

Learn why we charge membership fees

We get it - no one likes a content blocker. Take one extra minute and find out why we block content.

Not exactly the question you had in mind?

Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.

ask a question
very concise code and did the trick. The other answer was also very good. Thanks

Is it possible that the column can have an empty cells at the bottom, if so which column could be used to determine the last cell.

Could you post an example file with dummy data so we can test any script developed