Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-21
4
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

721 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