# add variable line count to formula

#### agiammo

##### Board Regular
I have a worksheet that is updated daily by a pivot table. The line count changes daily. I am updating a macro that will filter the data, sort it by cost, and then find the percentage of cost based on the total population. How do I get VB to tell me how many lines are in the worksheet, (subtracting one for the header) and then stick that value into my formula?

Here is the code leading up to this point.

'sort sheet by descending extended cost
Range("E1").Select
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
'Paste highest value in Cum Total Column
Range("E2").Select
Selection.Copy
Range("H2").Select
ActiveCell.PasteSpecial

'add formula to tally up Cum Cost all the way down (column H)
Range("H3").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+RC[-3]"
Dim i As Long
Dim z As Range
i = [F65536].End(xlUp).Row
[H4] = "=R[-1]C+RC[-3]"
[H4].Copy Range("H4:H" & i)
Set z = Sheets("All But CONS").Range("H4:H" & i)

I've been asked to use the following formula: H2/\$H\$###
where ### stands for the line count.

Any good ideas?

### Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
LineCount = Sheets("YourSheet").Range("A65536").End(xlUp).Row

change the YourSheet and the A65536 if you want to check for a different column than A.

Ok, this seems to work. But how do I extract the actual count? How could I paste the value to a new cell, like J2?

Thanks!

Hi,

LineCount = Sheets("YourSheet").Range("A65536").End(xlUp).Row
[J2] = LineCount

or just

[J2] = Sheets("YourSheet").Range("A65536").End(xlUp).Row

To use linecount in a formula:
[J2].Formula = "=H2/\$H\$" & linecount
This message was edited by rikrak on 2002-10-18 15:38

Cool! That works great!

I've been trying to add that value to my formula, and it is not working. I feel really dumb b/c this seems like it should be very simple.

I have tried this: =H2/\$H\$(J2)
and also : =H2/\$H\$[J2]

But XL doesn't like either of those. What am I doing wrong?

That's not a valid reference, try this

=H2/INDIRECT("\$H\$" & J2)

or,

=H2/OFFSET(\$H\$1,J2-1,0)

ok! I finally got this to work. I had to go back and use your first calculation method so the word 'linecount' was actually used.

Here is the code! It works! Thank you!!!!!

'add formula to tally up Cum Cost all the way down (column H)
Range("H3").Select
ActiveCell.FormulaR1C1 = "=R[-1]C+RC[-3]"
Dim i As Long
Dim z As Range
i = [F65536].End(xlUp).Row
[H4] = "=R[-1]C+RC[-3]"
[H4].Copy Range("H4:H" & i)
Set z = Sheets("All But CONS").Range("H4:H" & i)

'calculate linecount and place value in cell J2
LineCount = Sheets("All But CONS").Range("A65536").End(xlUp).Row
[J2] = LineCount

'[J2] = Sheets("All But CONS").Range("A65536").End(xlUp).Row

[I2].Formula = "=H2/\$H\$" & LineCount

Replies
7
Views
295
Replies
2
Views
199
Replies
0
Views
202
Replies
2
Views
223
Replies
2
Views
259

1,211,710
Messages
6,103,452
Members
447,866
Latest member
bowers261

### 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.

### Which adblocker are you using?

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

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