Consulting

Results 1 to 7 of 7

Thread: Find and rename Table in Workbook if it is present

  1. #1
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location

    Find and rename Table in Workbook if it is present

    Check if Table is existing in workbook and change its name if it is:

    I need to determine with vba if a specific Table name, e.g. "ZA_Table", is already used in the workbook, if it is, I want the code to change the name of the existing Table to "OldTable".

    1) How can this be done?
    2) How can I determine what the full address of the table is, i.e. sheet name and range?
    Range("ZA_Table").Address
    only gives the range.

    Thank you,
    Kind Regards
    vanhunk

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim ws As Worksheet
        Dim t As ListObject
        
        On Error Resume Next
        
        For Each ws In Worksheets
            Set t = ws.ListObjects("ZA_Table")
            If Not t Is Nothing Then
                t.Name = "OldTable"
                MsgBox t.Range.Address(external:=True)
                Exit For
            End If
        Next
        
    End Sub

  3. #3
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    @mana:
    Thank you very much, your code does the job and you also gave me another idea. Please have a look at the code below (also working fine).
    Sub toets2()
    Dim Adres As String
    
    On Error Resume Next
        Adres = Range("ZA_Table").Address(external:=True)
    
        Range(Adres).ListObject.Name = "OldTable"
    
    MsgBox Adres
    End Sub
    Regards
    vanhunk

  4. #4
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Add some checking for existing table and error checking. If the code is adding a table and the table already exists, it just adds a number to it, without warning or informing you. If you reference the new table with the name you gave it, it wont work because of the additional number at the end.

    Regards,
    vanhunk

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Maybe try this


    Option Explicit
    Sub test()
    Dim ws As Worksheet 
    Dim ws As Worksheet
    Dim t As ListObject 
    Dim t As ListObject
    On Error Resume Next 
    For Each ws In Worksheets 
    Set t = ws.ListObjects("ZA_Table")
    Set r = ws,ListObjects("OldTable")
    If Not t Is Nothing Then
    If Not R is Nothing Then
    MsgBox "OldTable exists!" VBOkOnly
    Exit Sub
    Else
    t.Name = "OldTable"
    MsgBox t.Range.Address (external=:True)
    Exit For
    End If
    End If
    End Sub

    [/CODE]
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        If [isref(ZA_Table)] Then ListObjects("ZA_Table").Name = "old_table"
    End Sub

  7. #7
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Thank you VBAX Guru and snb, I appreciate your inputs!

    Regards,
    vanhunk

Tags for this Thread

Posting Permissions

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