Excel VBA: Selection.Columns.Count does not work with multiple selections

For example, if you select Range("A1") and press CTRL and select Range("B3"), the code Selection.Columns.Count will return 1.  How do I get it to return 2?

Thank you,

Troy
T1080Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
You need to address the areas in teh selection i.e~

msgbox ColumnCount(selection)

Chris
Function ColumnCount(rng As Range)
Dim intArea As Integer
    
    For intArea = 1 To rng.Areas.Count
        ColumnCount = ColumnCount + rng.Areas(intArea).Columns.Count
    Next

End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne Taylor (webtubbs)Commented:
You'll need to iterate through the selected Areas....

    Dim Area As Range, colCount As Long
    For Each Area In Selection.Areas
        colCount = colCount + Area.Columns.Count
    Next
    MsgBox colCount

Wayne
0
Wayne Taylor (webtubbs)Commented:
Yeah, what Chris said...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Chris BottomleySoftware Quality Lead EngineerCommented:
:o)

Snuck in by a few milliseconds

Chris
0
T1080Author Commented:
I appreciate the help!!!
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Glad we could help and an excellent choice for acceptance - there being no time between the two posts.

Chris
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.