Forum Discussion
chowell97
Feb 26, 2023Copper Contributor
Changing table name breaks VBA code
Hello, I have an excel sheet that is used as a template to create additional sheets in an excel workbook. This sheet has a table named Master_Template. There is VBA code that references the ...
- Feb 26, 2023
Does each sheet have only one table? If so, you could use code like this:
Dim Tbl As ListObject Dim Module As Range Dim MACHINE As Range Set Tbl = ActiveSheet.ListObjects(1) Set Module = Tbl.ListColumns("TYPE").DataBodyRange Set MACHINE = Tbl.ListColumns("MACHINE").DataBodyRange
HansVogelaar
Feb 26, 2023MVP
Does each sheet have only one table? If so, you could use code like this:
Dim Tbl As ListObject
Dim Module As Range
Dim MACHINE As Range
Set Tbl = ActiveSheet.ListObjects(1)
Set Module = Tbl.ListColumns("TYPE").DataBodyRange
Set MACHINE = Tbl.ListColumns("MACHINE").DataBodyRange
chowell97
Feb 26, 2023Copper Contributor
Hello Hans,
No, there is a second table in the sheets but the table name is not referenced in any code.
Only this Master_Table is referenced by name.
Thanks in advance.
- HansVogelaarFeb 26, 2023MVP
In the Visual Basic Editor, activate the Immediate window (Ctrl+G).
Type
? ActiveSheet.ListObjects(1).Name
and press Enter.
If you see Master_Table, you should be able to use the code as posted by me.
If not, change ListObjects(1) to ListObjects(2) in the code.
- chowell97Feb 26, 2023Copper ContributorThank you for the additional tip!
- chowell97Feb 26, 2023Copper ContributorThank you for your help, your suggestion worked as intended!