5

A non-IT related class has been assigned a group project where the work they do will be stored is a single .xlsx file. The members decided the best way to collaboratively edit said file would be to split it into its constituent sheets, upload each *.xlsx sheet to an SVN repository, and use locks and a .txt file to organize sheet/member responsibility.

The group has accomplished the splitting of said files with a VB script (courtesy of this wonderful site) which was as follows:

Sub SaveSheets()
Dim strPath As String
Dim ws As Worksheet

Application.ScreenUpdating = False

strPath = ActiveWorkbook.Path & "\"
For Each ws In ThisWorkbook.Sheets
    ws.Copy
    'Use this line if you want to break any links:
    BreakLinks Workbooks(Workbooks.Count)
    Workbooks(Workbooks.Count).Close True, strPath & ws.Name & ".xlsx"
Next

Application.ScreenUpdating = True
End Sub

Sub BreakLinks(wb As Workbook)
    Dim lnk As Variant
    For Each lnk In wb.LinkSources(xlExcelLinks)
        wb.Breaklink lnk, xlLinkTypeExcelLinks
    Next
End Sub

Therewith the group now has a repository where each member is currently in the process of editing their respective files. The question then is, how can we automate the re-unification of these files into one .xlsx file with the preservation of the original links.

EDIT 4/2: started bounty // I'm aware that the links were "broken" by the above script but am not exactly sure what this means though I suspect it would make re-assembly with the preservation of original links more difficult. It should be noted that the original file which had the links is still available and might could be used to assist with this problem.

EDIT 4/2: Excel version is 2010--original links do not exist in current files.

EDIT 4/3: Original links are not in the current files, but it is desired that with the re-unification the original links (from original unedited file, pre-splitting) be re-created/preserved.

eichoa3I
  • 51
  • 6
  • Thanks for the edit. Please check whether links exist in the original workbook. From your ribbon you can check this by examining _Data > Connections > Connections_ and _Data > Connections > Edit Links_ (in Excel 2007). Please also indicate which version of Excel you are using. – Taliesin Apr 02 '13 at 19:01
  • Excel 2010; no links exist – eichoa3I Apr 02 '13 at 19:58
  • What do you call "the preservation of the original links"? – Ilya Kurnosov Apr 03 '13 at 06:37
  • @kurniliya the links that were present in the original file, which was the file before the splitting. – eichoa3I Apr 03 '13 at 17:33
  • Could you upload a file with some of those links??? I'd like to study them, but don't know how to create them in a sheet. (What kind of links they are) – Daniel Möller Apr 03 '13 at 17:58
  • Do you have a sheet in this workbook that was not being split up, say an overview sheet? If so, does this still exist in the original? And lastly is this sheet to remain unchanged? – glh Apr 06 '13 at 10:22

5 Answers5

1

If you have SharePoint, you can all update the same Excel (2003 or 2010) book.

http://office.microsoft.com/en-us/excel-help/about-shared-workbooks-HP005262294.aspx

James Jenkins
  • 1,954
  • 1
  • 24
  • 43
1

We probably need more detail in order to help you, but you may be able to accomplish what you need as follows (maybe this can kick-start a solution):

  • Loop through the sheets in the workbook
  • For each sheet
    • Open the appropriate xlsx file
    • Identify non-formula cells
    • For each of those cells
      • Copy to the identical location in the main workbook
    • Close the xlsx file

Below is an example (based on your SaveSheets code). If you try this, be sure to backup everything first. We obviously don’t know how the spreadsheets are laid out and how they are used. It would really suck if it got all screwed up. Also, there are some assumptions:

  • The layout and used range in the xlsx files are the exact same layout and used range as it appears in the original workbook.
  • The links you are referring to are formulas (either to another sheet or another workbook).

If these assumptions are wrong, you will need to modify as appropriate (identifying specific ranges to copy and/or adding more robust logic to the routine).

Note that the actual code to do this is very short. I added comments and basic error handling which significantly increased the amount of code.

This code would be added to the original workbook.

Sub RetrieveSheets()
Dim strPath As String
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim wbk As Workbook
Dim rng As Range

Application.ScreenUpdating = False

strPath = ActiveWorkbook.Path & "\"
For Each ws In ThisWorkbook.Sheets

    'Open the xlsx file (read only)
    Set wbk = Nothing
    On Error Resume Next
        Set wbk = Workbooks.Open(strPath & ws.Name & ".xlsx", ReadOnly:=True)
    On Error GoTo 0

    'Continue if xlsx file was successfully opened
    If Not wbk Is Nothing Then
        Set ws2 = Nothing
        On Error Resume Next
            Set ws2 = wbk.Worksheets(ws.Name)
        On Error GoTo 0

        'Continue if appropriate sheet was found
        If Not ws2 Is Nothing Then
            'Identify cells to copy over (cells that are constants)
            For Each rng In ws2.Cells.SpecialCells(xlCellTypeConstants)
                'set the cell value equal to the identical cell location in the xlsx file
                If (Left(ws.Range(rng.Address).Formula, 1)) <> "=" Then
                    ws.Range(rng.Address) = rng
                End If
            Next
            Set ws2 = Nothing
        End If

        'Close the xlsx file
        wbk.Close False
    End If
Next
Set wbk = Nothing
Application.ScreenUpdating = True

End Sub

Dave Thornton
  • 581
  • 3
  • 7
  • Thanks for the reply! To confirm, this would be added to the original .xlxs file (before I split it, and hence the one that doesn't have anyone's work in it)? – eichoa3I Apr 04 '13 at 00:01
  • Links refer to another sheet in the workbook (the first sheet). I took you to mean that I should a) open the original file (pre-splitting, contains blanks, no work), b) open all of the worked on sheets, c) make the module, run it. I did this and Eh, all I know to say is that nothing happened. – eichoa3I Apr 04 '13 at 00:07
  • Regarding your first question, yes the code needs to be placed in a module within the original xlsx file. As to the second, I suspect it couldn't find the worked on files. You may have to change strPath to reflect the right location. Also, you don't need to have them open, if you do it may prompt you with a warning. You could also comment out the "on error" lines and run it again to see where it breaks. If you like, let me know what it tells you and I'll help you troubleshoot. – Dave Thornton Apr 04 '13 at 02:47
  • This most likely will not work because my code only copies cells that match locations containing values in the original file. If the original file is blank where work is done, it will not be copied over. Without knowing the structure, it's difficult to selectively copy. Therefore, this solution will not be a simple plug and play. You would need to modify it or post a sample. If there is a specific range where each sheet is worked on, the code could be changed to target that. – Dave Thornton Apr 04 '13 at 03:00
  • I changed the code so that it copies all cells from the exported worksheets while skipping cells that are formulas in the main workbook. My approach to the solution attempts to preserve work that was done after splitting. If that is not a big issue, I think jeedo's approach is a clean way to go (i.e. do something during the splitting process that makes it easier to recombine later). – Dave Thornton Apr 04 '13 at 14:02
  • How can I post a sample of this file for you? I feel I don't understand the exel file's structure myself well enough to verbalize what I'm trying to accomplish. – eichoa3I Apr 04 '13 at 22:05
1

The links then don't really apply in the solution, as you said the original doesn't have any links and so reassembly with links isn't required.

The script provided even has a comment embedded saying "Use this line if you want to break any links:". So if you comment the line below out (prepend the line with a ') it will preserve the links in the child workbooks.

Using the answer to a previous question on copying sheets to another workbook reassembly can be accomplished with the following VBA:

Sub CombineSheets()
Dim strPath As String
Dim ws As Worksheet
Dim targetWorkbook As Workbook

Set targetWorkbook = ActiveWorkbook


Application.ScreenUpdating = False

'Adjust path location of split files
strPath = "C:\code\xls-split"

Dim str As String

    'This can be adjusted to suit a filename pattern.
    str = Dir(strPath & "\*.xl*")
    Do Until str = ""
        'Open Workbook x and Set a Workbook variable to it
        Set wbResults = Workbooks.Open(strPath & "\" & str, UpdateLinks:=0)
            For Each Sheet In ActiveWorkbook.Sheets
              Sheet.Copy After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
            Next Sheet
        wbResults.Close SaveChanges:=False
        str = Dir()
    Loop
Application.ScreenUpdating = True


End Sub

This will append the other workbooks to the currently opened workbook.

Sourced Replacing FileSearch function for code to find xls files in a directory.

Community
  • 1
  • 1
jeedo
  • 361
  • 2
  • 5
  • 1
    No I'm sorry for the confusion, but the original *did* have links. When we decided to split the file into all of its sheets the links "broke" meaning, when someone would open the file they'd get an error message which said links were broken (still usable, but annoying) so I sourced another solution which is the code in the OP which more correctly broke (?) the links. After that the error message wasn't displayed, but!--the original file *did* have links, and it is my intent that the recreated one should too. – eichoa3I Apr 04 '13 at 00:48
  • The result of breaking the links is that [the current value of the cell is replaces the formula to the link](http://office.microsoft.com/en-au/excel-help/break-links-HP003065606.aspx). So yes it's annoying that there's a popup message but in order to preserve the links when merging back in, you have to retain them on the way out. Can you give some examples of the links and what information they provide? – jeedo Apr 04 '13 at 04:33
  • I have an alternative solution but it depends on they type of formula you have that refers to external workbooks. It means that on splitting, the formula are re-written as text but without more information I can't tell if that breaks the rest of the sheet. – jeedo Apr 04 '13 at 05:16
  • How can I post a sample of this file for you to examine? I don't think I understand it well enough to explain it. – eichoa3I Apr 04 '13 at 22:05
  • many ways to share it, eg. via skydrive and make the file public or even via google drive. I'm pretty sure you can make files public via dropbox too. – jeedo Apr 05 '13 at 02:47
  • this seems to work well although there are some errors. First I can show you how the files are [structured](http://i.imgur.com/8H8Y6fq.jpg) in a folder called tmp. When the script is [run](http://i.imgur.com/EtIkaPF.jpg) Excell 2010 displays this [message](http://i.imgur.com/AK4Rv2r.jpg) -- select 'no' and things appear to work although you will see [this](http://i.imgur.com/QScNvgr.jpg), [and this](http://i.imgur.com/EUexYP7.jpg). Aside from that all appears to work (after updating link sources), except for [this little problem w/ the images](http://i.imgur.com/TQRVG25.jpg). – eichoa3I Apr 08 '13 at 20:16
  • This website http://spreadsheet1.com/excel-vba-bugs.html suggests it is a bug in 2010 that causes this issue. – eichoa3I Apr 08 '13 at 23:28
0

This is a rough outline of how I accomplished this:

  1. Use Office2013 and not office 2010
  2. create tmp/ directory with original .xlsx assignment file.
  3. create source/ directory in tmp/
  4. use split sheets module (listed on this page), but comment out the line that break links.
  5. place all of the resulting .xlsx files into source/ (you can delete the original .xlsx file)
  6. remote the first sheet from the sources/ folder, and place it ../
  7. Open this first sheet, and import/use the 'combinesheets' module listed on this page.
  8. Save the sheet, reopen it and you'll be prompted to update links. Do so, and select "change source" and select the first sheet Re in step 6.
  9. links will automatically update; done.

Notes: you'll have to save a file as macro enabled, ...

eichoa3I
  • 51
  • 6
0

Take a look at this MSDN Article on Merging Data from multiple workbook (.xls/.xlsx) files

http://msdn.microsoft.com/en-us/library/office/gg549168%28v=office.14%29.aspx

I don't know much about VBA but I think this is what you are looking for.

Also note it does get rid of the need for a text file to manage the files

adam2510
  • 563
  • 1
  • 7
  • 22