PDA

View Full Version : Object visibility?



CDTech
04-29-2010, 10:53 AM
Hello all.

Can anyone tell me why this works
Worksheets(sThisSample).Activate
Application.Range("A1").Activate

'Count returns long data type, cast to integer.
iAnswer = CInt(WorksheetFunction.Count(Worksheets(sThisSample).Range("A2:A8000")))


yet this doesn't

Worksheets(sThisSample).Activate
Application.Range("A1").Activate

'Count returns long data type, cast to integer.
iAnswer = CInt(WorksheetFunction.Count("A2:A8000"))


I assume since worksheet sThisSample is in scope, the count function will use the range A2:A8000 on that worksheet. It works fine in an earlier section of code where the starting sheet is active.:dunno

mbarron
04-29-2010, 12:20 PM
With this:
iAnswer = CInt(WorksheetFunction.Count("A2:A8000"))
you are trint to count a string ("A2:A8000"), not the range "A2:A8000"

use:
iAnswer = CInt(WorksheetFunction.Count(Range("A2:A8000")))

CDTech
04-29-2010, 02:28 PM
With this:
iAnswer = CInt(WorksheetFunction.Count("A2:A8000"))
you are trying to count a string ("A2:A8000"), not the range "A2:A8000"

use:
iAnswer = CInt(WorksheetFunction.Count(Range("A2:A8000")))

All those emoticons and there's not a facepalm.

Okay, I fixed that dumb error yet I still have to specify the worksheet when using Worksheetfunction.Count. I guess it doesn't pay attention to which worksheet is in scope and expects it to be explicit in the call even though there's no error message if you don't spec it.:dunno

Thanks for the help.

mbarron
04-29-2010, 03:14 PM
Your first line of code will evaluate to 0 since there a 0 numbers in the string.

How about this one?
:omg2:

Type
:OMG2:

CDTech
04-30-2010, 07:52 AM
Your first line of code will evaluate to 0 since there a 0 numbers in the string.

How about this one?
:omg2:

Type
:OMG2:

Um, yeah. Symptomatic of my inability to successfully proofread my stuff.

As for my first line of code, since the function expects a range object, it didn't occur to me I had to explicitly pass it a range object. I assumed it would resolve the range address as the range object I wanted to process.

I STILL need a book that discusses these kinds of subtle nuances that keep tripping me up. Or accept that I can't program my way out of a wet paper sack and should just give up.