Solved

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

Posted on 2013-11-21
4
350 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

763 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