Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on 

Create a table with VBA and name it

Is there a way with VBA in Excel to programically create a table and name is?  I am pulling in data from an ACCESS 2003 database, placing it in a worksheet called DataSource, but I also need to then create a table of the data and be sure its name is always DataSource.  I tried recording a macro, but naturally it first names is an auto-generated name, but is there a way around this?  This is Excel 2010 VBA.

Sandra
Microsoft DevelopmentMicrosoft ApplicationsMicrosoft Excel

Avatar of undefined
Last Comment
dlmille
Avatar of dlmille
dlmille
Flag of United States of America image

Do you have a sample, sanitized workbook to work from?

Dave
Avatar of Sandra Smith
Sandra Smith
Flag of United States of America image

ASKER

actually Dave, I got a version to work and was just going to delete this question.

Sandra
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sandra Smith
Sandra Smith
Flag of United States of America image

ASKER

Very similar.

Sandra
Avatar of Sandra Smith
Sandra Smith
Flag of United States of America image

ASKER

Actually, taking a look at your version, it was a bit simplier than what I had so did use it.  I was using naming a range, but I like the ListObjects add method better than what I did.

Sandra
Avatar of dlmille
dlmille
Flag of United States of America image

I would suggest unlisting any table that might exist, prior to making a new one - assuming you're refreshing your data and the table stub of the object is still there:
Sub makeTable()
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim r As Range
Dim lastCol As Long
Dim lastRow As Long

    Set wkb = ThisWorkbook
    Set wks = wkb.Sheets("DataSource")
    
    lastCol = wks.Cells(1, wks.Columns.Count).End(xlToLeft).Column
    lastRow = wks.Cells(wks.Rows.Count, 1).End(xlUp).Row
    
    'get range of table
    Set rng = wks.Range("A1", wks.Cells(lastRow, lastCol))
    
    'convert to table list object
    On Error Resume Next
    wks.ListObjects("Table1").Unlist
    On Error GoTo 0
    wks.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = "Table1"
End Sub

Open in new window


Cheers,

Dave
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo