VBA Naming a table with a variable(sheet name)

dannyh

Board Regular
Joined
Oct 15, 2007
Messages
156
Hi All

I need to run a piece of code once or twice a month on a sheet (with that months data) then the following month run the same code on a new sheet(with that months data)

Within my code I create a table that is then referenced in a summary section, I need each table on each sheet to have a different name as I have found otherwise the summary section picks up values from previous months sheet.

I have tried a few different options with my last being to define two parts of the table name (one fixed and one variable) as strings and then put them together, I am now getting the following error

Function call on left hand side of the assignment must return Variant or Object.

I think this is because I have tried to define my variable as a string but I'm not sure and after messing about with the code for a day I need help

VBA Code:
Dim shtName As String
    shtName = ActiveSheet.Name
Dim tblName As String
    tblName = "_AgingTable"
    
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = _
        shtName & " AgingTable"
    
    Range("I3").Select
    ActiveCell.Formula = "=SUM(J3:O3)"
    Range("J3").Select
    ActiveCell.Formula = "=SUMIFS(Nov_19_AgingTable[Amount Due],Nov_19_AgingTable[Overdue Days],""<1"")"
    Range("K3").Select
    ActiveCell.Formula = "=SUMIFS(Nov_19_AgingTable[Amount Due],Nov_19_AgingTable[Overdue Days],"">=1"",Nov_19_AgingTable[Overdue Days],""<=30"")"
    Range("L3").Select
    ActiveCell.Formula = "=SUMIFS(shtName & tblName[Amount Due],shtName & tblName[Overdue Days],"">=31"",shtName & tblName[Overdue Days],""<=60"")"

This the piece of code in question with the error occurring on the bottom line, I removed the two strings and hard typed the correct table name in the lines above to prove that it worked correctly.

Can anyone tell me what my error is ?

Thanks Dan
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try it like
VBA Code:
Dim shtName As String
    shtName = ActiveSheet.Name
Dim tblName As String
    tblName = shtName & "_AgingTable"
    
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = _
        tblName
    
'    Range("I3").Formula = "=SUM(J3:O3)"
'    Range("J3").Formula = "=SUMIFS(Nov_19_AgingTable[Amount Due],Nov_19_AgingTable[Overdue Days],""<1"")"
'    Range("K3").Select
'    ActiveCell.Formula = "=SUMIFS(Nov_19_AgingTable[Amount Due],Nov_19_AgingTable[Overdue Days],"">=1"",Nov_19_AgingTable[Overdue Days],""<=30"")"
    Range("L3").Formula = "=SUMIFS(" & tblName & "[Amount Due]," & tblName & "[Overdue Days],"">=31""," & tblName & "[Overdue Days],""<=60"")"
 
Upvote 0
Hi Fluff

Thanks for your quick response I have changed my code accordingly but am still getting a run-time error 1004 - Application-defined or Object-defined error on the same line.

Dan
 
Upvote 0
Did you change this line
VBA Code:
tblName = shtName & "_AgingTable"
& this one
VBA Code:
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = _
        tblName
as well, or just the line with the formula?
 
Upvote 0
yes my new code is this

VBA Code:
    Dim shtName As String
    shtName = ActiveSheet.Name
    Dim tblName As String
    tblName = shtName & "_AgingTable"
    
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = _
        tblName
           
    Range("I3").Formula = "=SUM(J3:O3)"
    Range("J3").Formula = "=SUMIFS(Nov_19_AgingTable[Amount Due],Nov_19_AgingTable[Overdue Days],""<1"")"
    Range("K3").Formula = "=SUMIFS(Nov_19_AgingTable[Amount Due],Nov_19_AgingTable[Overdue Days],"">=1"",Nov_19_AgingTable[Overdue Days],""<=30"")"
    Range("L3").Formula = "=SUMIFS(" & tblName & "[Amount Due]," & tblName & "[Overdue Days],"">=31""," & tblName & "[Overdue Days],""<=60"")"
 
Upvote 0
In that case check that your two column headers are correct.
 
Upvote 0
They are, I double checked but I assume the two lines above wouldn't work if they were wrong ?

I have included a small snapshot of my data, the overdue days have been adjusted in order to produce a value if the formula works correctly, does this work for you ?

Thanks Dan

DateCustomerAmount DueDue onOverdue Days
29-Nov-2019A34220.0029-Nov-2019
1​
29-Nov-2019B2790.0029-Nov-2019
1​
29-Nov-2019C217991.0129-Nov-2019
5​
29-Nov-2019D12992.0029-Nov-2019
11​
29-Nov-2019E27140.0029-Nov-2019
12​
28-Nov-2019F2237.0028-Nov-2019
18​
27-Nov-2019G285273.9027-Nov-2019
29​
27-Nov-2019H29500.0027-Nov-2019
29​
26-Nov-2019I25960.0026-Nov-2019
33​
26-Nov-2019J29500.0026-Nov-2019
35​
25-Nov-2019K11800.0025-Nov-2019
40​
25-Nov-2019L20060.0025-Nov-2019
55​
25-Nov-2019M64900.0025-Nov-2019
55​
25-Nov-2019N70800.0025-Nov-2019
58​
25-Nov-2019O43660.0025-Nov-2019
59​
25-Nov-2019P53100.0025-Nov-2019
60​
21-Nov-2019Q4140.0021-Nov-2019
62​
20-Nov-2019R23600.0020-Nov-2019
68​
20-Nov-2019S94400.0020-Nov-2019
70​
20-Nov-2019T2230.0020-Nov-2019
70​
20-Nov-2019U82600.0020-Nov-2019
73​
19-Nov-2019V162424.6419-Nov-2019
76​
 
Upvote 0
1591735263564.png
1591735183050.png


I'm really confused now, when I check the line of code that errors it looks correct, here is a side by side of the line above and the line errors.
 
Upvote 0
That suggests that your sheet is called Nov'19 rather than Nov_19, which would explain things.
 
Upvote 0
Hi Fluff

It does indeed, that's what happens when you have been starring at the screen too long !

It doesn't like the apostrophe but if I rename my sheet Nov_19 it works fine, any ideas why as a matter of interest ?

Thanks for the help !
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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