0

What I want to do

I want a code in my workbook (wbDestination) that opens another workbook (wbBosOriginal) and copies an entire sheet as values (wbBosOriginal has a lot of code in it, in modules and in the worksheet in question, and I do not want this code because it references stuff in wbB that doesn't exist in wbDestination). I have had great problems pasting as values, because it will not paste columns and rows that are currently hidden. So this is why I decided to import the whole sheet instead.

What I tried and what's wrong with it

Here is a block of code I used to copy the worksheet in the destination workbook, in the last index position. The problem with it is that some links still exist to the old workbook (Formulas, validation lists, conditionnal formatting). I have deleted all these links but STILL when I paste the sheet successfully, save and reopen, I have an error saying some content is unreadable. I believe there are still some elements linked to the old workbook.

Set wbBosOriginal = Workbooks.Open(strChosenPath, ReadOnly:=True)
With wbBosOriginal.Sheets("BOS")
        .Visible = True

        'Pastes the ws in last position in wbDestination
        .Copy after:=wbDestination.Sheets(wbDestination.Worksheets.Count)

        Set wsNewBos = Worksheets(Worksheets.Count)

        'Deletes VBA code in the copied sheet
         ThisWorkbook.VBProject.VBComponents.Item(wsNewBos.CodeName).CodeModule.DeleteLines 1, _
            ThisWorkbook.VBProject.VBComponents.Item(wsNewBos.CodeName).CodeModule.CountOfLines

    End With

The worksheet is successfully pasted with no code in it, with everything else it had previously. I then remove all formulas, conditionnal formatting, and validation lists. Even after removing those as well, I still get an error when opening the workbook.

My question

Apart from conditional formatting, validation lists, VBA code, and formulas linking a worksheet that was pasted to a new workbook, what other elements could cause the workbook from opening in repair mode every time due to existing links to the old workbook?

If my question is not clear, comment and I will clarify.

David G
  • 2,315
  • 1
  • 24
  • 39

3 Answers3

1

Dealing directly with VBE seems a bit heavy-handed to me. If your code is manipulating several workbooks, I would put the code in an add-in and not have it in any workbook. (Technically *.xlam addins are workbooks, but when I say "workbook" I mean normal *.xls, *.xlsx, *.xlsm, etc.)

That said, if you're just copying cell values (which may be formulas) between different workbooks, you shouldn't have any dependencies other than cell references, named ranges, and user-defined functions in the original workbook. I would make sure there are none of those. Please also share how you are ensuring your formulas do not have broken references.

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
  • Upvoted for mentioning named ranges, which I had not thought of. I believe it contributes to finding the answer. However I ended up using a different method. See other comments for what was holding me back. – David G Jun 30 '16 at 14:29
  • Good old cell visibility issues! Endlessly inconvenient – JamesFaix Jun 30 '16 at 14:38
0

If the issue you are having is caused by trying to avoid hidden columns and rows not allowing pastevalues, why not unhide the rows and columns and then copy only the values to the new book?

Just cycle through each of the sheets in the original book and use the method .UsedRange.Hidden = False. As far as I am aware, this should unhide every cell on the sheet and allow you to do the original pastevalues calls

RGA
  • 2,577
  • 20
  • 38
  • It doesn't for several reasons. First off, UsedRange is very unreliable and doesn't detect the real used range in my sheet. See http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba Second, in the original workSheet (I am only copying 1 of the sheets) there are row groupings. When the workbook is opened, the groupings may or may not be closed. Even setting all rows to hidden = false leaves them with height = 0, and changing row heights of 2000 rows takes forever even with `.rows("1:2000").RowHeight = 18`. Rows with 0 height don't seem to get copied. – David G Jun 30 '16 at 13:55
  • I have managed to unhide all rows by first expanding ALL row groups with `.Outline.ShowLevels RowLevels:=3`. I am now able to copy/paste as values without skipping rows. For some reason, having groups screws with the `.Hidden` property! Glad to have figured this out. I'm still leaving the question open because it's still valid. – David G Jun 30 '16 at 14:22
  • @DavidG Glad you found a solution! – RGA Jun 30 '16 at 14:24
0

This works fast and smooth (it's harder to delete ALL the data Imo):

Sub tests()
    Dim AllRange As Range: Set AllRange = ActiveSheet.UsedRange
    Dim ItemRange As Range
    Dim myWbDestination As Workbook: Set myWbDestination = ThisWorkbook
    Dim SheetDestination As String: SheetDestination = ("Sheet2")
    For Each ItemRange In AllRange
    With myWbDestination.Sheets(SheetDestination)
    .Range(ItemRange.Address) = ItemRange.Value
    End With
    Next ItemRange
End Sub

Repair mode can be triggered by many factors, you would need to post the code you are getting to look for an explanation, it would be like asking why vba may broke enter image description here

Sgdva
  • 2,800
  • 3
  • 17
  • 28
  • That just changes the formulas to values. I am already doing that. My question is "are there other things than formulas, validation lists, conditionnal formatting, and vba code that keep a link to the original workbook that got copied". – David G Jun 30 '16 at 13:49
  • That was to satisfy easier the original problem _I have had great problems pasting as values, because it will not paste columns and rows that are currently hidden._ As I stated, there are many factors that could break into repair mode some could be: 1. [Many merged cells and the user sorts the range: Bug fixed with Excel 2013 Service Pack 1 2.](http://www.spreadsheet1.com/how-to-diagnose-excel-file-corruption-and-repair-workbooks.html) , some other [errors](http://geekswithblogs.net/msoffice/archive/2013/05/02/top-5-scenarios-that-cause-corruption-in-ms-excel-file.aspx) – Sgdva Jun 30 '16 at 14:07
  • I'm 99% sure it is not due to a bug. I ended up copy and pasting everything as values by first unhiding all cells. It seems obvious but the problem was due to row groupings, which prevent the `.Hidden = false` to work properly. Instead I used `.Outline.ShowLevels RowLevels:=3` to expand all groups first. After that I was able to unhide and paste as values. I'm still leaving the question open as I'd like to know what can leave trace links behind when copy/pasting. – David G Jun 30 '16 at 14:26
  • [Name manager](http://www.excel-easy.com/examples/names-in-formulas.html) and [Connections](https://msdn.microsoft.com/en-us/library/office/ff196294.aspx) are the most frequent cases – Sgdva Jun 30 '16 at 14:32