[VBA] underscore in ListObject Name and Formula error

Holiner

New Member
Joined
May 20, 2019
Messages
2
Hello

I have a macro that create automatically new Sheets and on each Sheet create a Table
The Table Name is based on Sheet Name
Code:
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:G1"), , xlYes).Name = "Tab_" & ActiveSheet.Name
Excel replace space and other characters on the Sheet name with underscore _
Company X & Y became Tab_Company_X___Y

I want to make some synthesis from all the Table but my Formula doesn't work
Code:
TabName = Sheets("Company X & Y").ListObjects(1).Name
MyFormula= "=COUNTA(" & TabName & "[xxxxx])"       '[xxxxx] is the header name of a column
Cell(1, 2).Formula = MyFormula

This code work only if the Sheet name doesn't contain space or special characters

if I list all the ListObjects of my Workbook, those who have been renamed automatically appear without the underscore : Tab_Company X & Y :confused::huh:
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You can store in a "control" sheet the name of the sheet and its name of Table.
As shown below:

<table style="font-family:Arial; font-size:12pt; border-style: groove ;border-color:#0000FF;background-color:#fffcf9; color:#000000; "><tr><td ><b>Sheet Control</b></td></tr></table>
<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:94.1px;" /><col style="width:131.17px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="background-color:#ffff00; font-weight:bold; text-align:center; ">SHEET</td><td style="background-color:#ffff00; font-weight:bold; text-align:center; ">TABLE NAME</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >Sheet1</td><td >Tab_Sheet1</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >Company X & Y</td><td >Tab_Company_X___Y</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >company b c</td><td >Tab_company_b_c</td></tr></table>

With the following macro you can fill out the "Control" sheet.
On the red line put the special characters, they will be replaced by "_"

Code:
Sub test3()
    Dim shc As Worksheet, sh As Worksheet
    Dim i As Long, j As Long, hwName As String
    
    chars = [COLOR=#ff0000]Array("&", "-", " ")    'put special characters[/COLOR]
    i = 2
    Set shc = Sheets("Control")
    For Each sh In Sheets
        If sh.Name <> shc.Name Then
            shc.Cells(i, "A").Value = sh.Name
            wName = sh.Name
            For j = 0 To UBound(chars)
                wName = Replace(wName, chars(j), "_")
            Next
            shc.Cells(i, "B").Value = "Tab_" & wName
            i = i + 1
        End If
    Next
End Sub

---
Now create the table name with the following:

Code:
Sub CreateTable()
    Dim shc As Worksheet, wName As String, b As Range
    
    Set shc = Sheets("Control")
    Set b = shc.Range("A:A").Find([COLOR=#0000ff]ActiveSheet.Name[/COLOR])
    If Not b Is Nothing Then
        wName = b.Offset(0, 1)
        ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:G1"), , xlYes).Name = wName
    End If
End Sub

---
And in this way you can use your code:

Code:
Sub test2()
    TabName = Sheets("Company X & Y").ListObjects(1).Name
    MyFormula = "=COUNTA(" & TabName & "[G])"      '[xxxxx] is the header name of a column
    Cells(1, 10).Formula = MyFormula
End Sub
 
Upvote 0
Re: [Solved] [VBA] underscore in ListObject Name and Formula error

I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top