How to reference a column name in a VBA formula?

caprie13

New Member
Joined
Sep 3, 2022
Messages
1
Office Version
  1. 2019
  2. 2010
As of now I have an IF function in VBA that references column locations - eg. IF(R4=1,"Complete",SUM(AB4:AB1000))

There will be situations where I can't reference columns R or AB because sometimes columns get deleted. I would need to reference columns called CompletionLevel (this is column R normally, but won't always be) and Quantity (this is column AB normally, but won't always be). The column headers that I want to reference will always be in row 3.

I think I'll need to use the Find function but I'm not totally sure how to write what I need in VBA. Would I need to define these columns at the start of my code, and how would I specify that I want it to look at the cell right under the column header (aka R4 and AB4:AB1000)?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I think I'll need to use the Find function but I'm not totally sure how to write what I need in VBA. Would I need to define these columns at the start of my code, and how would I specify that I want it to look at the cell right under the column header (aka R4 and AB4:AB1000)?
Try something like this. I've assumed that you want the formula to go in column "D" in the first row under the header row.

VBA Code:
Sub InsertFormula()
  Dim CLcol As String, Qcol As String
  
  Const HeaderRow As Long = 3 '<-Adjust as required
  
  With Rows(HeaderRow)
    CLcol = Split(.Find(What:="CompletionLevel", LookAt:=xlWhole).Address, "$")(1)
    Qcol = Split(.Find(What:="Quantity", LookAt:=xlWhole).Address, "$")(1)
  End With
  Range("D" & HeaderRow + 1).Formula = Replace(Replace(Replace("=IF(#^=1,""Complete"",SUM(%^:%1000))", "^", HeaderRow + 1), "#", CLcol), "%", Qcol)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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