View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Don Don is offline
external usenet poster
 
Posts: 487
Default Determining number of Ranges

Jimmy,

Can't tell you the best way to get but I can suggest why you're getting the
answer you're getting....I'm sure someone will jump in here and give you a
one liner or two to accomplish your goal.

In your macro, your macro does not test for data, it only tests to see if
that one Range is valid. Check this out by clearing all data and commenting
out the Range values in your macro. You'll get the same answer...."1". You
need to loope through each Range, testing for data. Add 1 to a variable that
starts at "0" for each range that has data, then check the variable.value,
you'll find the answer will be three.

HTH, and if it doesn't, like I said, someone will put us both straight....:)
Don


"Jimmy O" wrote:

I'm new to VBA and I have a worksheet where I need to determine the number of
contiguous blocks of cells with data in them. There could be two to four
blocks. All of the blocks will be within Range A1:I25. I thought I could use
Areas.Count so I set up a test Sub with three blocks of cells. Here is the
sub:

Sub CountAreas()
Range("A1:C7").Value = 9
Range("F9:I16").Value = 10
Range("A21:D25").Value = 12
Range("A1:I25").Select
x = Selection.Areas.Count
Range("A30").Value = x
Debug.Print x
End Sub

The value of x is 1 when I run the sub. I thought it should be 3. Any help
on what I'm doing incorrectly would be apprciated.