I created a VBA script that deletes an existing data table (named Data) and replaces it with a new version (that has the same headers and is named Data). I have a lot of formulas and charts that rely on this table and was wondering how I can keep the references working after changing the table (so that pivot tables, formulas, and array formulas update automatically when I update the Data table).
Below are some pictures of the problems I am running into:
Pivot tables not updating with new data
Data turning to #REF! after deleting Data table
Likely what is happening is that once you delete the table (based on your definition of the problem), the link to the chart is then lost.
Rather than delete the table (as a whole), delete the individual data rows:
Sub RemoveTableBodyData()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("Table1")
'Delete Table's Body Data
If tbl.ListRows.Count >= 1 Then
tbl.DataBodyRange.Delete
End If
End Sub
from: The VBA Guide To ListObject Excel Tables
This should otherwise keep the table reference intact for the chart. FYI: because the data has been updated, you may want to make a call out to refresh any dependent pivots and charts, otherwise it may not be clear the data has updated... I might make the call to refresh once after the data set has been cleared, then again after the update... this will make it clear the pivots/charts have been updated.
Good Luck.
Use a dynamic named range as the pivottable source. Then only remove data below the headers.Don't overwrite or change the headers
Or use your existing table but again keep the headers and only delete rows from beneath (e.g. ActiveSheet.ListObjects(1).DataBodyRange.Delete
) . If you delete the entire table then any dependant formulas will error as the reference is no longer valid. That table no longer exists.
Or have code that creates everything for you.
So, why not change all the formulae to text by using edit/replace "=" with "xyxyxy" and then do whatever with your data as everything is just text.
Once done replace "xyxyxy" with "=" and the formulae work again - works fine with my sheets and I have a LOT of formulae in them.
Late to the game but a simple solution I found was to change the table reference to use indirect. Example Table1[Field1] changes to indirect("Table1[Field1]").
Related
I have a worksheet that is xlSheetVeryHidden and protected VBA to prevent the user from accessing the worksheet. Contained in that worksheet is a table with sensitive data. However, I noticed that someone can find the table name and return the tables contents by simply typing in a cell = and then the first letter of the table name.
Is there a way I can prevent the table from appearing in the formula bar?
As far as I'm aware of you can't.
https://answers.microsoft.com/en-us/msoffice/forum/all/toggling-range-name-visibility/a6c2af7f-d9ee-4f63-989a-2233bee8a50d
One other interesting fact is that, if you only had one table and nothing else in the workbook ThisWorkbook.Names.Count will be zero. Which means that Table names are not counted as names at all.But if you try adding a new item in names collection using existing table name you'll get an error.
One more thing, you could try using [TableNameHERE].ListObject.Name = ChrW(&H4FFF) and be clever about it, considering limitations.
I have table-like data, and I'm looking to make a chart that displays that data. Since the data is that formula-generated from other data in the workbook, I don't know in advance how many lines it will have. I want to make a chart that adapts to this data.
Up to this part of the question, I could use named ranges to solve this.
The thing with the solution with named ranges is that it does not scale well when I have many of these charts. I have a Python script that generates CSV files that I import into my workbook as a sheet, and I don't want to have to know in advance how many of them there will be, or what they will be named. I only want to be able to import the CSV files into a new or existing sheet, and copy-paste the formulas from another working sheet, as well as be able to replace the existing CSV data with new data.
With named ranges, I would have to manually create a named range for each series of each sheet, and I would have to use the sheet's name so that the named range can be visible to the whole workbook (in order to analyze the data in a global sheet) without any name conflict. This is (more or less) acceptable when I do these steps myself now, but if I want to redo this in a few months, or if I pass my workbook to someone else, we wouldn't know why it's not working with a new sheet.
So is there any way to get this done without delving into VBA stuff? I'm using a recent version of Excel.
Per the comments, try using pivot tables, making your range extend far beyond expected results and filtering out "blanks" in your pivot and generate your chart from that. The default pivot chart is ugly but you can remove buttons and format as needed. Just remember to refresh your pivot during every update period.
I have an Excel workbook that has ~100 sheets in it, and each sheet is a table whose headers are exactly the same. I have edited the text of the headers in my blank "to copy" sheet, but I now need to copy those changes across each and every existing sheet (the rest of which have data in them that I cannot lose/change).
I have already tried using Shift+click to select all the sheets, but cannot edit the header because they are already formatted as tables and I get the "Cannot make changes to a table or XML mapping when multiple sheets are selected." error. So I either need a way to select every sheet and make them not a table anymore (temporarily) so that I can make the bulk change, or find some other way of doing this change while they are still a table.
Thanks for your time and advice.
Mirror a single table to multiple sheets in excel using vba
You might see this solution, As it is not possible to change multiple sheets while they are formatted as tables .. you might do the changes if your sheets are not formatted as sml or tables.
The issue that I am having is when I copy my table in excel(which is generated from running a query) and then special paste it into a power point slide.
The paste works but when I run the query in excel with different parameters it brings back a varying number of rows. The table in the slide will update using the same number of row from the original copy/paste. If the table grew it will only have up the the original number of rows, if it shrinks i get this.
The data is removed but the rows are still there
I found an answer on answers.microsoft.com.
The key is to create a named range and you have to use a workaround if your named range is an Excel table. I'll cover this workaround.
Let's say that you have a query that returns a table named DataTable. If you want to link to this table in PowerPower, first you'll need to create a named range that does not directly refer to a table.
Create a new Named Range under Formulas > Name Manager and name it TableAlias with a formula referencing a specific range, e.g., =Sheet1!$A$1.
Once you've created this alias, save the file and then paste the range referenced by your named range reference (Sheet1!$A$1, not the table you eventually want) into your PowerPoint slide using Home > Paste > Paste Special... > Paste Link > Microsoft Excel Worksheet Object. If things are working correctly at this point, you should be able to go to File > Edit Links to Files and see that the link is using the named range instead of an R1C1 range.
Now that it knows to use the named range, let's change the formula for TableAlias in the Name Manager to the range we actually want to link, =DataTable. Once the formula is updated and you've saved the document, the link in PowerPoint should update to this new range giving you a dynamic link to a table.
This process seems a bit finicky, but I did getting it working for me where if I added or deleted rows, then the PowerPoint slide would update the table range appropriately.
I have defined a table in MS Excel and want an exact copy of this table on another worksheet which updates itself when changes are made on the original table. For instance, if one inserts a new line at the end of the original table, excel auto-expands this table - this should also be reflected on the table copy.
Is this possible without VBA?
Thanks in advance!