How do I count the number of columns in an Excel table in VBA ?


I'm trying to count the number of columns in a table in an Excel 2010 VBA application  called 'Sales' but getting a 'Variable not defined' error.

Do tables have to be defined separately from sheets ? If so, how ?

My code is as follows .....

Dim lgColumnCount As Long
lgColumnCount = Range(Sales).Columns.Count

Who is Participating?
yuppyduConnect With a Mentor Commented:
Try this

lgColumnCount= Sheet1.Range("Sales").Columns.Count

replace Sheet1 with whatever sheet the table is
I think you are missing some data in your command line. First of all it should be Range("Sales") and you are also not saying  which sheet you are referring to...
Can you share the workbook?
TocogroupAuthor Commented:
Ah ! It was that simple. Just the quotes were missing.
Thanks for the solution.
Well sometimes the simplest things make a big difference....
Have a good day
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.