Question concerning Areas Property

Mindpsyche

Well-known Member
Joined
Mar 19, 2012
Messages
760
Hello everyone,

In my self learning of vba I stumbled upon a neat website with some nice VBA beginer tips: so i found this website via a linkedin group.

This is the website: http://www.excel-spreadsheet.com/vba/vba.htm

So in one of the sections I found a code that explains the Areas property.

However, when i copy paste this code to the vba editor it gives an error.

Something with the range selection:

This is the code:

Code:
Sub AreaExample()
    Sheets(1).Select
    UsedRange.Select
    MsgBox Selection.Area.Count 'Number '3' - ranges returned
End Sub

Can someone please explain whats wrong with this?

Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I think you are missing an "s".

Code:
Sub AreaExample()
    Sheets(1).Select
    UsedRange.Select
    MsgBox Selection.Area[COLOR="Red"]s[/COLOR].Count 'Number '3' - ranges returned
End Sub
 
Upvote 0
I think you are missing an "s".

Code:
Sub AreaExample()
    Sheets(1).Select
    UsedRange.Select
    MsgBox Selection.Area[COLOR="Red"]s[/COLOR].Count 'Number '3' - ranges returned
End Sub

Hi Mike,

Thanks for your help.

It still returns 1 to me instead of 3.

This is how my sheet looks:


Excel 2007
ABCDEFGH
112
212
3
4
5
612
712
8
9
10
11
1212
1312
Sheet1


So I have 3 non-contigous ranges for which the areas function should give me 3 right? Instead I only get one as the answer.

Here is my adjusted code:

Code:
Sub AreaExample()
    Sheets(1).Select
    ActiveSheet.UsedRange.Select
    MsgBox Selection.Areas.Count 'Number '3' - ranges returned
End Sub

I'm trying to understand the functionality of the areas function. As i have read it should return the number of non-contigous ranges within a selection.

By the way the actualy code from the website was:

Code:
Sub AreaExample() 
    Range("A1:B2", E4, G10:J25").Select 
    MsgBox Selection.Area.Count 'Number '3' - ranges returned
End Sub

Which also gives me an error...at the Range("A1:B2", E4, G10:J25").Select step.
 
Upvote 0
Try this

Code:
With Sheets(1)
   
    With .UsedRange
        MsgBox "Used Range: " & .Address & vbCr & .Areas.Count
    End With
    
    With .Cells.SpecialCells(xlCellTypeConstants)
        MsgBox "Cells with entries: " & .Address & vbCr & .Areas.Count
    End With

End With

P.S. the error in the code from the website is the " after B2
 
Upvote 0
Try this

Code:
With Sheets(1)
   
    With .UsedRange
        MsgBox "Used Range: " & .Address & vbCr & .Areas.Count
    End With
    
    With .Cells.SpecialCells(xlCellTypeConstants)
        MsgBox "Cells with entries: " & .Address & vbCr & .Areas.Count
    End With

End With

P.S. the error in the code from the website is the " after B2

Thanks a lot Mike!
 
Upvote 0

Forum statistics

Threads
1,207,392
Messages
6,078,223
Members
446,323
Latest member
fishezuk

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