WorksheetFunction.Count HELP NEEDED!

oakwood_001

Board Regular
Joined
Apr 4, 2008
Messages
108
Hi All,

I'm trying to assign the number of cells (blank or non blank) in a given range to a variable but everytime I try, I get a runtime 1004 error.

Can you help?

Code:
Dim strStartingConCell As String
Dim strStartingUniqueCell As String
Dim x As Integer
Dim y As Integer

strStartingConCell = InputBox("Starting Concatenated Cell")
strStartingUniqueCell = InputBox("Starting Unique Cell")

y = WorksheetFunction.Count(Range(strStartingConCell, Selection.End(xlDown))
x = WorksheetFunction.Count(Range(strStartingUniqueCell, Selection.End(xlDown))
Code:

I think you get what I'm trying to do from this (I hope).

I also tried selecting each cell first. Then tried selecting the range first but it didn't like Count(Range(Selection))

Any help is very much appreciated.

Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
selection will refer to active cell... is this what you want or do you want:

Code:
y = WorksheetFunction.Count(Range(Range(strStartingConCell),Range(strStartingConCell).End(xlDown))
 
Upvote 0
First, you shouldn't use Integers as you might have more cells than that - use Long instead.
Second, you wouldn't use Range(Selection) - Selection is a range, so you just use Selection.
Third, if you want to count the number of cells, regardless of whether they are empty, then you don't want the COUNT worksheet function since that only counts numbers. You want the Range object's Count property.
 
Upvote 0
Thanks DonkeyOte

I don't get the error now but both x and y are equal to 0 when running this.

They should be equal to ~3000 and ~2000 respectively.

Is it because of my strings? The input box asks for a cell referenfce - i.e. Q4

Would that therefore work?
 
Upvote 0
So rorya,

I could just use:

y = Range(Range(strStartingConCell), Range(strStartingConCell).End(xlDown)).Count

And that would do it?
 
Upvote 0
Hurray!

Thanks to both of you - rorya - wouldnt have realised I needed to use the Count property of the range and - Donkey - wouldnt have realised thats how I needed to specify the range

:biggrin::biggrin::biggrin:
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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