Consulting

Results 1 to 5 of 5

Thread: Formatting table in worksheet

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    2
    Location

    Formatting table in worksheet

    Hi,

    Each I have to format my new excel file containing from 10 to 30 worksheets. At this time I have to go each worksheet one by one to format.
    Here is a macro what I did in one worksheet7 named "Jason Data":

    Sheets("Jason Data").Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$C$3"), , xlYes).Name = "Table7"
    Range("Table7[#All]").Select
    ActiveSheet.ListObjects("Table7").TableStyle = "TableStyleMedium9"

    How can I do it by looping all the worksheets and format them in the way above. Starting range is always A1 but ending range will change on each worksheet. Can we use a variable for "Table7"?
    In other words, will ActiveSheet.ListObjects(myvariable).TableStyle = "TableStyleMedium9" work?

    Best,

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Table range = Range("A1").CurrentRegion
    For i = 1 to sheets.count 
    TableName = "Table" & Cstr(i)
    
    ActiveSheet.ListObjects(TableName).TableStyle =...
    Next i
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    2
    Location
    I have tried it but getting this error --> Run-time error '9': Subscript out of range at Activesheet.........


    Dim TableName As String
    Dim i As Integer

    For i = 1 To Sheets.Count
    TableName = "Table" & CStr(i)
    Sheets(i).Select
    ActiveSheet.ListObjects(TableName).TableStyle = "TableStyleMedium9"
    Next i

    Not sure what's wrong?

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Aren't you supposed to add and name a listobject? You do in your first post
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I'd keep it simple

    This loops all worksheets and then loops all tables on the worksheet


    Option Explicit
    
    Sub FormatAllTables()
    
        Dim ws As Worksheet
        Dim lo As ListObject
    
        Application.ScreenUpdating = False
        
        For Each ws In ActiveWorkbook.Worksheets
            For Each lo In ws.ListObjects
                lo.TableStyle = "TableStyleMedium9"
            Next
        Next
        
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •