Solved

Format Table via VBA

Posted on 2011-09-27
12
276 Views
Last Modified: 2012-06-27
Sub Macro1()
    
    Dim lEndrow As Long, rng1 As Range
    lEndrow = Sheets("Payroll Summary").Range("A" & Sheets("Pivots").Rows.Count).End(xlUp).Row
   
    Set rng1 = Sheets("Payroll Summary").Range("A1:lEndRow")
    ActiveSheet.ListObjects("rng1").TableStyle = "TableStyleMedium1"
    
End Sub

Open in new window


Hi,

I have the above macro designed and some what toyed with :o

When I run it, it gives an error (as expected)

   Set rng1 = Sheets("Payroll Summary").Range("A1:lEndRow")

I really suspect the error is at the end. Could anyone please help me integrate the last row into this formatting?

Much appreciated!
0
Comment
Question by:Shanan212
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 13

Author Comment

by:Shanan212
ID: 36712503
Even when the range is correct in other way, the next line gives error 'subscript' out of range

I used this to test it out (but I do want to define the end of range

Set rng1 = Sheets("Payroll Summary").Range("A1:C99")
0
 
LVL 33

Assisted Solution

by:Norie
Norie earned 200 total points
ID: 36712508
Which columns do you want to format?

You can try this but it will only be for column A:

Range("A1:A" & lEndRow)

If you wanted to format columns A:F you would use something like this.

Range("A1:F" & lEndRow)
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36712570
That solves the range error but I think I am defining a range and then treating it as a table.

In that case, how would I define a range as a table?

Much appreciated!
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 42

Accepted Solution

by:
dlmille earned 300 total points
ID: 36712621
Try this:

 
Sub convertToTable()
Dim wkb As Workbook
Dim sht As Worksheet
Dim tblRng As Range

    Set wkb = ThisWorkbook
    Set sht = wkb.Sheets("Payroll Summary")
    
    Set tblRng = sht.Range("A1:C" & sht.Range("A" & sht.Rows.Count).End(xlUp).Row)
    
    sht.ListObjects.Add(xlSrcRange, tblRng, , xlYes).Name = "Payroll_Table"
End Sub

Open in new window


See attached...

Dave
Copy-of-GL-Summary---Master-File.xls
0
 
LVL 33

Expert Comment

by:Norie
ID: 36712645
What exactly do you mean by a table?

If you actually want to format a listobject you just need it's name, you don't need to define it's range.

I'm pretty sure that name isn't 'rng' which you are using in the code, and is what is causing the error.

You need something like this:

ActiveSheet.ListObjects(<TableName>).TableStyle = "TableStyleMedium1"

Replace <TableName> with the name of the table.

If there's only one table on the sheet you can use ListObjects(1).
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36712722
The code I provided creates the table - from that point, you can use the table's name to affect changes, as imnorie as suggested.
0
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 36712767
Imnorie,

I could do that but the table get deleted every time I run another macro. So I have to 'define' a table on VBA side and then assign a formatting!

Dave,

Quick question,

Where in your coding are you assigning the formatting?
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36712798
I didn't - just went with the defaults.

here's with the formatting:

Sub convertToTable()
Dim wkb As Workbook
Dim sht As Worksheet
Dim tblRng As Range
Dim mytable As Object

    Set wkb = ThisWorkbook
    Set sht = wkb.Sheets("Payroll Summary")
    
    Set tblRng = sht.Range("A1:C" & sht.Range("A" & sht.Rows.Count).End(xlUp).Row)
    
    sht.ListObjects.Add(xlSrcRange, tblRng, , xlYes).Name = "Payroll_Table"
    Set mytable = sht.ListObjects("Payroll_Table")
    mytable.TableStyle = "TableStyleMedium1"
End Sub

Open in new window

0
 
LVL 13

Author Comment

by:Shanan212
ID: 36712875
Thanks! This sort of info is hard to find over internet!
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36712901
Sometimes I just record a macro while I do stuff I'd never coded before.  After that, I start googling :)

Dave
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36713254
Yup! I do that too :)
0
 
LVL 33

Expert Comment

by:Norie
ID: 36713379
If you don't know the name of the table and it is the only one on the activesheet you can use ListObjects(1).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

696 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