Solved

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

Posted on 2013-11-21
4
313 Views
Last Modified: 2013-11-21
Hi,

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

Thanks
0
Comment
Question by:Tocogroup
  • 3
4 Comments
 
LVL 4

Expert Comment

by:yuppydu
ID: 39665230
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?
0
 
LVL 4

Accepted Solution

by:
yuppydu earned 500 total points
ID: 39665238
Try this

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

replace Sheet1 with whatever sheet the table is
0
 

Author Closing Comment

by:Tocogroup
ID: 39665250
Ah ! It was that simple. Just the quotes were missing.
Thanks for the solution.
0
 
LVL 4

Expert Comment

by:yuppydu
ID: 39665254
Well sometimes the simplest things make a big difference....
Have a good day
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now