add variable line count to formula

agiammo

Board Regular
Joined
May 9, 2002
Messages
68
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
Selection.sort Key1:=Range("E2"), Order1:=xlDescending, Header:=xlGuess, _
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 did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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.
 
Upvote 0
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!
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 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
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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