Excel VBA - Count number of rows and columns in range

Status
Not open for further replies.

gui132

New Member
Joined
Aug 26, 2019
Messages
5
I want to get the number of rows and columns in my "selection" range, but for some reason Columns.Count returns 1 when I have multiple columns selected.
Anyone knows why?
I am using excel 2013 32 bit.

Code:
[COLOR=#333333]Sub test()[/COLOR]

[COLOR=#333333]Dim sourceSht As Worksheet[/COLOR]
[COLOR=#333333]Dim targetSht As Worksheet[/COLOR]
[COLOR=#333333]Dim Table As ListObject[/COLOR]
[COLOR=#333333]Dim LastRow As Long[/COLOR]
[COLOR=#333333]Dim selection As Range[/COLOR]


[COLOR=#333333]Set sourceSht = ActiveWorkbook.Sheets("Colaboradores")[/COLOR]
[COLOR=#333333]LastRow = sourceSht.ListObjects("Table3").Range.Columns("BB").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row[/COLOR]
[COLOR=#333333]Set targetSht = ActiveWorkbook.Sheets("Sheet3")[/COLOR]


[COLOR=#333333]'Check for existance of table[/COLOR]
[COLOR=#333333]'SELECT ALL NEEDED DATA[/COLOR]


[COLOR=#333333]Set selection = sourceSht.Range("Table3[UE i" & vbLf & "(área)], Table3[UE ii" & vbLf & "(unidade)],Table3[2013 -1ºSemestre]:Table3[2019-2ºSemestre]").Offset(-1, 0)[/COLOR]
[COLOR=#333333]Debug.Print selection.Columns.Count <<<<<<<< Here the console returns 1 ( also the number of rows does not change with or without the offset, which is weird to me too)[/COLOR]


[COLOR=#333333]'COPY AND PASTE INTO NEW SHEET[/COLOR]
[COLOR=#333333]selection.Copy[/COLOR]
[COLOR=#333333]targetSht.Range("A1").PasteSpecial xlPasteValues[/COLOR]


[COLOR=#333333]'CREATE NEW TABLE[/COLOR]
[COLOR=#333333]Debug.Print h[/COLOR]
[COLOR=#333333]Set selection = targetSht.Range("A1").Resize(w, h)[/COLOR]
[COLOR=#333333]w = selection.Rows.Count[/COLOR]
[COLOR=#333333]h = selection.Columns.Count[/COLOR]
[COLOR=#333333]Debug.Print h[/COLOR]
[COLOR=#333333]targetSht.ListObjects.Add(xlSrcRange, selection, , xlYes).Name = "MyTable"[/COLOR]

[COLOR=#333333]End Sub[/COLOR]
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Status
Not open for further replies.

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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