Counting Number of Columns

Bluepolo3

New Member
Joined
Sep 26, 2011
Messages
12
I am using Excel 2010. I have tried using the code:

Num_Col = Cells(1, 1).End(x1ToLeft).Column

to count the number of columns that have data and keep getting an error. Is this code incorrect? If so can anyone please help me out to count the number of columns being used.

Thanks
 

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.
I use this, not sure if 2010 is different (I'm in 2007)

.Cells(1, Columns.Count).End(xlToLeft).Column
 
Upvote 0
Bluepolo3, your formula is using a number 1 instead of a letter l in the constant 'xlToLeft' -- and in your case you'd want 'xlToRight' to make it work.

In order to do xlToLeft, it would look like rdw7277's answer above (no period at the beginning tho, i think?)
 
Last edited:
Upvote 0
Welcome to the forum!

Ryan's method works fine if you wanted the column number in row 1 with data.

Code:
Sub Text_FindLastColumn()
  MsgBox FindLastColumn
End Sub

'Find the last used Column on a Worksheet:
Function FindLastColumn() As Integer
  Dim rLastColumn As Range
  If WorksheetFunction.CountA(Cells) > 0 Then
    'Search for any entry, by searching backwards by Columns.
    FindLastColumn = Cells.Find(what:="*", After:=[A1], _
                     SearchOrder:=xlByColumns, _
                     SearchDirection:=xlPrevious).Column
    Exit Function
    FindLastColumn = 1
  End If
End Function

Howsoever, if you really wanted the number of columns being used then:
Code:
acitvesheet.usedrange.Columns.Count
This could mean that column A or others up to the first used column are not counted.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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