Move sheets containing tables between workbooks without breaking named ranges - excel

-- Edit: this is now part of the bigger question of how to reliably move sheets about --
I've got a workbook which has sheets containing tables and sheet-scoped named ranges. Some of these sheets' formulas link to some of the names on other sheets (i.e. =Sheet1!Sheet1LocalName somewhere on Sheet2).
The time has come for me to create VBA code that moves these sheets into another workbook. Doing so, I of want all existing functionality to remain intact.
When looping over all sheets individually and Worksheet.Move -ing them one at a time to the other workbook, the range name links between the formulas get broken. E.g. when Sheet2 uses a name on Sheet1 in one of it's formulas:
Move over Sheet2;
Sheet2 will still correctly link to Sheet1 back in the source workbook.
Move over Sheet1 itself;
Excel 'helpfully' creates a workbook-scoped name of the same name for me as the name Sheet2 linked to (even if it didn't exist before), where
this new name does point back to the already moved sheet in the destination workbook, after which
the already moved Sheet2's links get modified to point to this new workbook-scope name in the source workbook, thus
messing up the link beyond repair.
Even if I could overcome this by analyzing the formulas beforehand to scan all sheet dependencies, since sheets may have links going both ways between them, it seems I can't do it this way.
When moving all sheets in one go using ThisWorkbook.Worksheets(Array(name1, name2)).Move, I get the Excel error "You cannot copy or move a group of sheets that contain a table".
So it seems I'm sorely out of luck here... There's of course the options of:
moving the sheets individually and rebuilding all formulas afterwards, and
replacing all tables with ranges and rebuilding all tables afterwards
but I'd understandably do not want to go there, since I do not control what goes on the sheets...
Any alternatives?
-- Edit --
The true purpose of this question is that I need to move the sheets over to a temp .xlsx workbook, save/close/re-open that workbook, and then move them back again to the original .xlsm workbook, thus scrubbing them off of their VBA module. See my other SO post for the background, though when I wrote that question I envisioned only needing to scrub the 1 topmost sheet in the ranged-name linking hierarchy, but it turns out I need to do it for all sheets to be safe enough.
After a lot of trial-and-error I found out that moving or copying all sheets in one go just isn't doable because of the tables on them, and handling them one at a time really messes up formulas and named ranges linking them together (even .Copy has similar unwanted side effects to using .Move). While I could in turn write code to 'fix' these broken names or delete these 'helpfully added' rogue names, I wouldn't be surprised if other range linking mechanisms (like chart source, pivot source, data validation list source, form control linked cells, etc.) also acted up badly, making this an even bigger mess to deal with...

Related

Dynamic Workbook Name changes and cell references - Is it possible?

my question is a pre-code question, as I would like to verify if and which is the best option to go about this - either formula, VBA or not possible.
I have a main workbook with an array of codes inside cells in sheet 1 - all in the format HXXX-XXX-XXX where X is numbers ranging from 0-9. Each code has an associated revision number as shown in the attached picture.
The workbook itself is named in the format: 'HXXX-XXX-XXX-YY Example Title' where YY can be any number starting from 0 to infinity (in theory). Only YY is subject to updates in the title. If changes are made in this workbook, then a new version is saved, with the YY changing in the title to the next consecutive number - Nothing else changes.
From this main workbook there are a large number of other workbooks (around 50) which share the same codes and revisions. My goal is to link all these workbooks up so that I only have to update a core main workbook and the rest of the codes (HXXX-XXX-XXX) revisions in all the other linked workbooks update accordingly.
The problem is that the other linked workbooks have a dynamic name in the format: 'HXXX-XXX-XXX-YY Example Title 2' where YY can be any number starting from 0 to infinity. YY gets updated to the next consecutive number if a change is made inside the document and this document is then saved as a separate document using Save As. NOTE: not all workbooks will get updated at the same time, as it all depends on which codes and therefore revisions are changing.
My question is firstly, is this possible with current excel functions to update links with ever-changing workbook links, which are saved as different files each time in the same folder?
Secondly, I have read about the INDIRECT function, but it is limited only to having the required workbooks opened at the same time as the updates to the main workbook, hence I am unsure that Formulas will work (I will be linking 50 workbooks to the main workbook) - From this I am gauging that VBA may be the best option, but I would like to double check this is possible before I begin attempting the code.
Of course the use of Formulas in the sheets would be much better, but because the file names are dynamic, and since the updated workbooks are saved as different files each time changes are made to it, I don't believe this is possible.
If I understand your problem correctly you need to differentiate between 3 things.
Using only Excel Formulas.
The problem with this is that formulas crossing workbooks have the big disadvantage, that you need to have the source-workbook open, otherwise manny formulas will not work correctly or update. You also have the disadvantage that saving the file needs still to be done automatically. The advantage is that you do not need VBA macros.
Using Power Query
With Power Query youmight be able to solve the problems with the dynamic names.You can even read a whole folder of files, combine them and do transformations without the need to open the files manually. But you still need to save new files manually and the files will only update once they are opened.
Using VBA
You can prettymuch do everything you asked in your problem description but VBA has its own disadvantages. Firstly the file-format ".xlsm" is not allowed by the IT department everywhere. Also to work, every user using the files needs to activate macros, otherwise the macros won't get executed. Therefore I think a VBA solution is better fitted if you only have a small circle of users or if you can execute the macros on your own (e.g. if the main workbook was updated, you need to execute a macro which opens, modifies and saves all affected files). After that you colleagues do not need VBA themself.
Possible solution:
To avoid VBA you could try to create a Power Query in each "other workbook". The query will read all filenames in the folder where the main workbook is located. Now you sort and filter for the workbook with the newest/highest ID/Titel automatically and load the result as an seperate Excel sheet (all that is possible with power query and without VBA).
Now you can use the "indirect" formula to use the ID/Titel to link to the newest main workbook. As mentioned above this will require that the main workbook is opened. Otherwise the indirect formula will not update the values. If that is out of the question, you probably need to go with VBA.

Comparing two listst of rows on different workbooks and updating list 2 with missing rows from list 1 - Excel VBA

What i try to accomplish in Excel using VBA is to have a "original" workbook that is used to start with each time, adjusted and saved under a new name. This workbook, and all workbooks that come from it have a "client database" sheet where client data is automaticly copied in to (1 row per client, A:G). Whenever a original workbook is filled in the data is copied to the client database in that specific workbook, but then automaticly saved under another name.
Therefore i am looking for a code that opens the original workbook (done), compares the database sheet of the new file with the original and then updates the original with the new client data. (Best case they would update each other). What's important is that the whole row should be compared, as for instance one company can have different contacts.
Now my coding skills unfortunately aren't good enough to accomplish this, and i have browsed different topics to find a solution but wasn't able to find a solution that completely covers the problem (and is written simple enough for me to still understand).
Both sheets hold the same database structure and the tab is called "Klanten database".
If anyone would be able and willing to help that would be much appreciated!
I have already tried different codes from the forum, but they all focus on either columns or specific cells and are made to extract unique value's to a new list or just to highlight the differences. Or are to complicated for me to understand.. :X
Sub UpdateOriginal()
'Open original file to update
Workbooks.Open Sheets("Data inhoud").Range("J12").Value & "\original.xlsm"
'compare active database with original and update the both of them
Workbooks("original.xlsm").Close SaveChanges:=True
End Sub```
None have worked :(

What is an invisible sheet named "D%$&01_DevSheet" doing in an Excel OOXML workbook?

I'm writing a program to read some data from an OOXML Excel Workbook using Apache POI that was provided to me as example input data. There is a strange sheet named D%$&01_DevSheet at the end. It is full of formula cells with weird formulas that reference most of the other sheets in the workbook. Here is an example of one of the cells:
'Horizontal Agreements'!S15+"8I/!%4\"
It is also not visible when the worksheet is opened in Excel.
I've never run across such a sheet before. It looks like some kind of internal Excel structure. Google searches for "D%$&01_DevSheet" and "excel" "DevSheet" have turned up nothing useful.
This sheet is not present in any test workbooks I've created. However, I'm not much of an Excel user and I did not exhaustively try all Excel features.
At this point, I'm going to hard code a rule that excludes sheets ending in _DevSheet from processing. However, since this stuff is turning up in my input, I think I should understand and handle it properly, so I have some basic questions:
What does Excel use sheets like D%$&01_DevSheet for?
What user actions cause them to be created?
Are they named regularly?
What is the most reliable way of detecting sheets like this Apache POI?
Normally Excel does not use such a sheet, so it seems to be added by some specific application or tool that was used to create this Excel workbook. It seems the developer of the application stores formulas and other things in a separate sheet for separation from the user-visible content.
So likely you will need to contact/research whichever application provided this file and get more information about this sheet from there.
You can probably only exclude these sheet by name as you already did.

How to pattern graphs on excel

I wanted to ask how it is possible to pattern graphs on the same sheet in excel. I am aware already that one can pattern the same graph with all its formulas by copying the whole sheet.
However I am looking for the pattern-action inside the same sheet, as it would result as very useful for me.
That means that I do once the whole work, along some given rows, which includes some graphs.
This whole content shall then be repeatedly copied down the columns, with the condition that the new graphs are locked on to the new destinations... Not the old ones.
Thanks for all input.
The series formulas for a chart contain absolute references and they cannot be changed to relative references. That means that when a chart is copied and pasted into the same worksheet, it will still refer to the original data range. The exception, as you noted, is copying the whole sheet, which will lead to the references being adjusted to the new sheet name.
When you copy a chart and paste it into the same sheet, then you need to adjust the references, either manually or with VBA.

Updating a workbook's existing structured table from an external master table?

Currently: I have a few sheets that contain one or more tables (e.g., Sheet1 has one table while Sheet2 has three tables). These same sheets are found in several end user workbooks. The tables in those sheets are used by structured references within cell formulas in the workbook.
Goal: I would like to have another workbook (a master workbook) that only has the sheets that contain the tables. Updates would be manually made to the master workbook i.e., add a row, delete a row, edit a row, add a column, delete a column, edit a column. Once updates are completed with the master workbook, the updated [master] sheets would then be placed into the various user workbooks, replacing the existing sheets and thus the tables (using VBA)...without breaking the structured references! No #REF errors! Well, this is what I'm envisioning, however, it doesn't have to be exactly this. Also, the common table sheets must be in the end user workbook, no external referencing! Unless it's part of the process to make what I need work, of course :-)
I'm very handy with VBA but not a MVP. Assume all of the workbooks -- both end user and master -- exist in the same directory. The master workbook would contain the code that would deliver the updated sheets to the end user workbooks. Excel 2007 & 2010 in Windows 7 environment.
I don't have any code as I'm trying to manually figure it out first without breaking anything. Suggestions welcomed! Thank you.
[Update 13Jun2012] Hopefully this more long-winded explaination will help.
What I'm doing:
I have a workbook that (essentially) copies itself into an end user workbook (1 to n times based on user form inputs). The end user workbook is protected to keep end users from making mistakes in certain formulas (various forms of sheet/cell protection). There are some sheets that contain tables and these tables are the same for all users. These tables are used for data validation (e.g., Allow: List; Source: =timing_droplist) and VLOOKUPs (e.g., =VLOOKUP($W8,Timing_table,FE$5+1,FALSE)). These end user workbooks are created once a year but updated three times during the year (they're for finance/budgeting). The tables can be updated at any time. Since data is added to the end user workbooks it's not convienent to re-generate those workbooks with the updated tables and then have the end user re-enter all their data. So it would better if the existing end user workbooks could have the common tables updated all together (using VBA).
What's happening (just ONE example):
"Timing" sheet exists and contains the table "Timing_table". If the master "Timing" sheet is added to the workbook, Excel renames it to "Timing (2)". This is normal. But the table in the added sheet becomes local instead of globle AND it, too, gets renamed, FOR EXAMPLE, "Timing_table12". I don't know how Excel comes up with the digits for the table name and they could change. If I delete the old "Timing" sheet all references to it are broken (of course) -- pulling up Name Manager shows #REF! in the value column. Renaming the new references doesn't fix the issue because the new references are local to the new sheet.
I tried manually cutting and pasting a table into another, pasting as Text, and that doesn't keep formulas (basically it performs a Paste as Values operation) and, overall, doesn't work. Just looking for options. External references to these tables is not an option.
I think that this thread might help you out: http://www.ozgrid.com/forum/showthread.php?t=66791
To provide you the reply directly (assuming that the problem mainly involves the external referencing):
Replied by Aaron Blood:
Re: Copy Formula To New Worksheet Without Path
You've already developed the code to do the copy/pasting to a new sheet and workbook. Now all you need to do is scan the formulas of the new worksheet and remove the ext refs from the formulas.
Run something like this at the end of your existing copy/paste macro...
VB:
Sub ExtRef_Remover()
Dim cell As Range, n As Variant
For Each cell In Workbooks("New_WB").Sheets("Sheet1").Cells.SpecialCells(xlFormulas)
n = Application.Find("]", cell.Formula)
If Not IsError(n) Then
cell.Formula = "='" & Right(cell.Formula, Len(cell.Formula) - n)
End If
Next cell
End Sub
Using extensive external references just didn't work out in this project so I convinced the project manager that we needed to change course. She agreed that what she wanted was too much to ask and taking too much time to implement, aside from compromising workbook generation and stability. I ended up using a data pull into the necessary tables via a command button. Waaaay easier to work with and stable.

Resources