Code that enters a dynamic formula

Andrew_Rossington

Board Regular
Joined
Feb 12, 2007
Messages
121
A while ago I asked for some help with creating code that would add a formula in a certain cell if the adjacent cell had a value in it.

I was very kindly offered the following solution:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Ce As Range, LstRw As Long

Application.ScreenUpdating = False
LstRw = Cells(Rows.Count, "B").End(xlUp).Row
LstRw2 = Cells(Rows.Count, "A").End(xlUp).Row
If LstRw < LstRw2 Then LstRw = LstRw2
If LstRw < 4 Then Exit Sub
For Each Ce In Range("B2:B" & LstRw)
    If Len(Ce.Value) = 0 Then
        Range("A" & Ce.Row).Value = vbNullString
    Else
        Range("A" & Ce.Row).Formula = "=Row() - 1"
    End If
Next Ce
Application.ScreenUpdating = True

End Sub

This works perfectly.

What I'm currently trying to achieve is to use the above code to add a formula whose cell reference changes depending on which row it's on.

For example, if there was a value in B2, this code could be used to enter"=COUNTIF(Classes!C:C,B2)" in cell A2. However, for further values in column B, the respective column A cell would still display the formula as entered. Is it possible to have the formula more along the lines of "=COUNTIF(Classes!C:C,B#)", whereby the reference in the formula represents the current row?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Andrew

Conceptually it's much easier to use FormulaR1C1 eg:

Code:
ActiveCell.FormulaR1C1 = "=COUNTIF(Classes!C3:C3,RC[1])"

Entered in a column A cell (eg A5) it will produce the formula:

=COUNTIF(Classes!$C:$C,B5)
 
Upvote 0
Hi Andrew

Conceptually it's much easier to use FormulaR1C1 eg:

Code:
ActiveCell.FormulaR1C1 = "=COUNTIF(Classes!C3:C3,RC[1])"

Entered in a column A cell (eg A5) it will produce the formula:

=COUNTIF(Classes!$C:$C,B5)

Hello Richard.

Thank you for the time and the solution. Whilst I had chance to implement this, and it worked fine, I had also figured out the following:

Formula = "=COUNTIF(Classes!C:C," & Ce.Address & ")"

Ultimately they both work, but would you be able to tell me what the key differences are between your way and mine?
 
Upvote 0
Well, the end result is pretty much the same either way, but I think using the R1C1 property just simplifies the formula you enter (ie no need to access the cell address or concatenate strings). At the end of the day just use what feels most comforatble to you!
 
Upvote 0
Well, the end result is pretty much the same either way, but I think using the R1C1 property just simplifies the formula you enter (ie no need to access the cell address or concatenate strings). At the end of the day just use what feels most comforatble to you!

Good advice!

Much appreciated, as always.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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