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?
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Juan Pablo González

MrExcel MVP
Joined
Feb 8, 2002
Messages
11,959
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.
 

agiammo

Board Regular
Joined
May 9, 2002
Messages
68
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!
 

rikrak

Active Member
Joined
Aug 21, 2002
Messages
255
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
 

agiammo

Board Regular
Joined
May 9, 2002
Messages
68

ADVERTISEMENT

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?
 

agiammo

Board Regular
Joined
May 9, 2002
Messages
68
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,224
Messages
5,594,915
Members
413,952
Latest member
JGer

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
Top