VBA formula without using RC[-x] designation

wingfield

New Member
Joined
May 19, 2013
Messages
12
hello,

I have a spreadsheet with say 6 columns. off to the right, I'm creating a formula in say cell m2, then copying that formula down until the end of the data. the formula in m2 would be something like a concatenate formula that uses two columns, say column b and column c. currently, my formula in m2 looks something like this:

ActiveCell.FormulaR1C1 = "=CONCATENATE(""I"",RC[-9])"

this example uses only one column and a hardcoded "I", but you get the picture.

my question is this. is there a way I can avoid using the RC[-x] format? I have column headings. is there a way the formula can simply look for the column heading to determine which cell to use at the top of that column and then copy it down?

don't worry about the copy down part. I can handle that. I'm simply looking for a way to get that first formula in cell m2 to use something other than those RC[-x] format and something more "permanent".

hopefully, this makes sense?

best regards,

david
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
If you don't want to use the R1C1 reference, then something like this
Code:
 ActiveCell.Formula = "=Concatenate(K1,C1)"

The formula will concatenate cells K1 and C1 for example

Add: To find a particular column header, you probably need to use the Range. Find method
 
Last edited:
Upvote 0
Example:

Code:
Sub Test()
    Dim Col As Long
    With ActiveSheet
        Col = .Rows(1).Find(What:="persfamID", After:=.Cells(1, .Columns.Count), LookIn:=xlFormulas, LookAt _
                :=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    End With
    ActiveCell.FormulaR1C1 = "=CONCATENATE(""I"",RC" & Col & ")"
End Sub
 
Upvote 0
Example:

Code:
Sub Test()
    Dim Col As Long
    With ActiveSheet
        Col = .Rows(1).Find(What:="persfamID", After:=.Cells(1, .Columns.Count), LookIn:=xlFormulas, LookAt _
                :=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    End With
    ActiveCell.FormulaR1C1 = "=CONCATENATE(""I"",RC" & Col & ")"
End Sub

thank you very much sir!!
 
Upvote 0

Forum statistics

Threads
1,224,416
Messages
6,178,501
Members
452,853
Latest member
philipnjk64

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