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?
 
Chris BottomleyConnect With a Mentor Commented:
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
 
Wayne Taylor (webtubbs)Connect With a Mentor AstronautCommented:
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)AstronautCommented:
Yeah, what Chris said...
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
Chris BottomleyCommented:
:o)

Snuck in by a few milliseconds

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

Chris
0
All Courses

From novice to tech pro — start learning today.