VBA formula help...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
Hi,

Mind's gone blank. Can someone remind me how I would write this:

Code:
    For i = 2 To LR
        With .Range("A" & i)
            .Formula = "=COUNTIF(RC[2]:[COLOR=#ff0000]R4913C[2][/COLOR],RC[2])"
            .Value = .Value
        End With
    Next i

So that the highlighted part always references the last cell based on the LR?

Thanks,

Matty
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try

.Formula = "=COUNTIF(RC[2]:R" & LR & "C[2],RC[2])"


Or to do it without looping, try
Code:
    With .Range("A2:A" & LR)
        .Formula = "=COUNTIF(RC[2]:R" & LR & "C[2],RC[2])"
        .Value = .Value
    End With
 
Last edited:
Upvote 0
Another way that I often find useful, particularly if LR needs to be incorporated in several places in the formula, is as follows.
Rather than breaking the formula up into bits and manually sticking LR in each relevant place is to write the formula as you want it but with a placeholder for LR then replace the placeholder(s) in bulk.
To me it has the added advantage of making the formula more readable.

Something like this
Code:
Const f As String = "=COUNTIF(RC[2]:R#C[2],RC[2])"

With .Range("A2:A" & LR)
    .Formula = Replace(f, "#", LR, 1, -1, 1)
    .Value = .Value
End With

Also, if we know we are staring in row 2 (which appears to be the case form your loop) and counting in column C then my Const statement would become ..
Code:
Const f As String = "=COUNTIF(C2:C$#,C2)"
.. which is much easier to read as it looks pretty much like a standard worksheet formula.
 
Upvote 0
Another way that I often find useful, particularly if LR needs to be incorporated in several places in the formula, is as follows.
Rather than breaking the formula up into bits and manually sticking LR in each relevant place is to write the formula as you want it but with a placeholder for LR then replace the placeholder(s) in bulk.
To me it has the added advantage of making the formula more readable.

Something like this

Code:
Const f As String = "=COUNTIF(RC[2]:R#C[2],RC[2])"  With .Range("A2:A" & LR)     .Formula = Replace(f, "#", LR, 1, -1, 1)     .Value = .Value End With
Also, if we know we are staring in row 2 (which appears to be the case form your loop) and counting in column C then my Const statement would become ..

Code:
Const f As String = "=COUNTIF(C2:C$#,C2)"

.. which is much easier to read as it looks pretty much like a standard worksheet formula.

Thanks Peter. That's a good tip!

Matty
 
Upvote 0
Another way that I often find useful, particularly if LR needs to be incorporated in several places in the formula, is as follows.
Rather than breaking the formula up into bits and manually sticking LR in each relevant place is to write the formula as you want it but with a placeholder for LR then replace the placeholder(s) in bulk.
I like that idea! Especially since, beside "neatening things up", it would also allow my one-liners to remain one-liners. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

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