1

Update: Thank you all - Having the spaces before and after the ampersands in the CountA solved the problem in one location, and being able to use the Row(Table) function suggested in the comments worked as well! Best community ever (I've been stalking this site to get ideas for about 9 months. This was my first post! It's been over 20 years since I coded, so you guys TOTALLY rock!

I have a macro that separates data into separate sheets by a category. It then turns the data on each sheet into a table with a different name for each sheet. I've used this code to do that:

nLastRowNewSheet = ActiveSheet.Range("B" & objWorksheet.Rows.Count).End(xlUp).Row
            ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$L$" & nLastRowNewSheet), , xlYes).Name = _
            "Table" & index
            ActiveSheet.ListObjects("Table" & index).TableStyle = "TableStyleLight12"

That part works great. The issue I'm having is that, when I try to create a macro to insert a formula to count the items in this list for a summary at the top of each sheet, I can't get the table number to automatically update the table number. I started by recording a macro that I'd planned up updating to make more dynamic.

ActiveCell.FormulaR1C1 = "=COUNTA(Table0[ID '#])"

I can't figure out how to change the 0 on the table reference to a dynamic value based on the index I've already used.

I tried the "table" & index like I used to create tables with different names,

   ActiveCell.FormulaR1C1 = "=COUNTA(Table"&index&"[ID '#])"

but I get a compile error: Expected End of Statement pop-up box.

Any suggestions on how to get that table number to update so that when the data is sorted if the stakeholders add or remove items from their tab the totals update.

Much thanks for any suggestions.

9
  • 2
    Try adding spaces before and after & (eg "=COUNTA(Table" & index & "[ID '#])" Commented May 14 at 11:37
  • ActiveCell.FormulaR1C1 = Replace("=COUNTA(Table0[ID '#])","0",index) Commented May 14 at 12:17
  • Is ID '# really the name of the column? Commented May 14 at 12:43
  • @Ike I would guess the column name is ID # the ' is to escape the hash. Commented May 14 at 14:14
  • 2
    When used without a leading space in VBA & acts as a "type hint" indicating to the compiler that the preceding text represents a variable of type Long. Adding a leading space before the & removes the confusion. Commented May 14 at 15:18

2 Answers 2

1

Just use the ListObject property to get the table:

ActiveCell.FormulaR1C1 = "=COUNTA(" & [A1].ListObject.Name & "[ID '#])"

where A1 is any cell within the table.

Sign up to request clarification or add additional context in comments.

Comments

0

If there is a single quote in the columns name (ID '#) then you have to escape it.

This should work:

ActiveCell.FormulaR1C1 = "=COUNTA(Table" & index & "[ID '''#])"

If there is only the # in the columns name (ID #) then it looks like this:

ActiveCell.FormulaR1C1 = "=COUNTA(Table" & index & "[ID '#])"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.