Consulting

Results 1 to 19 of 19

Thread: Copy Excel workbook without formulas??

  1. #1
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location

    Copy Excel workbook without formulas??

    Hi guys!

    I am trying to make my very first Excel macro and I have hit a snag. My background is Java, and this my first venture into anything VB or VB like...

    I have a workbook with multiple sheets. I need to copy it all to a new workbook with exception of the excel formulas - just the values, number formatting, background colours etc.


    The code I have so far does that, but only for the active sheet - can you help me out with looping through the rest?


    So far I have the following code working:

    [VBA]
    Sub test()

    Cells.Select

    Selection.Copy
    Workbooks.Add
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.Dialogs(xlDialogSaveAs).Show


    End Sub
    [/VBA]


    Can you guys help me with the loop?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings,

    Rather than copying/pastespecial, you might want to try something like:
    Option Explicit
        
    Sub exa()
    Dim wbNew As Workbook
    Dim wks As Worksheet
        
        Set wbNew = Workbooks.Add(xlWBATWorksheet)
        
        ThisWorkbook.Worksheets.Copy After:=wbNew.Worksheets(1)
        Application.DisplayAlerts = False
        wbNew.Worksheets(1).Delete
        Application.DisplayAlerts = True
        
        For Each wks In wbNew.Worksheets
            wks.UsedRange.Value = wks.UsedRange.Value
        Next
        
    End Sub
    Hope that helps,

    Mark

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub test()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim vecWS As Variant
    Dim i As Long

    Set wb = ActiveWorkbook
    ReDim vecWS(1 To wb.Worksheets.Count)
    For i = 1 To wb.Worksheets.Count

    vecWS(i) = wb.Worksheets(i).Name
    Next i

    wb.Worksheets(vecWS).Copy

    For Each ws In ActiveWorkbook.Worksheets

    ws.UsedRange.Value = ws.UsedRange.Value
    Next ws

    Application.Dialogs(xlDialogSaveAs).Show

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    wow - quick replies! Thanks!


    I will test this out - only way for me to learn this type of code :-)


    I hope none of these overwrite the sheet it is executed on :-)

    (my first venture stripped all from the original - lucky I had a backup :-) )

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Take another backup
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    Hi!


    This seem to do the loop with end save OK, but all cell which before had numbers, now screem "#VALUE!" at me.

    What went wrong???


    Quote Originally Posted by GTO
    Greetings,

    Rather than copying/pastespecial, you might want to try something like:
    Option Explicit
        
    Sub exa()
    Dim wbNew As Workbook
    Dim wks As Worksheet
        
        Set wbNew = Workbooks.Add(xlWBATWorksheet)
        
        ThisWorkbook.Worksheets.Copy After:=wbNew.Worksheets(1)
        Application.DisplayAlerts = False
        wbNew.Worksheets(1).Delete
        Application.DisplayAlerts = True
        
        For Each wks In wbNew.Worksheets
            wks.UsedRange.Value = wks.UsedRange.Value
        Next
        
    End Sub
    Hope that helps,

    Mark

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Webmonkey
    Hi!


    This seem to do the loop with end save OK, but all cell which before had numbers, now screem "#VALUE!" at me.

    What went wrong???
    Hi there,

    I am probably overlooking something, but I don't see how this could be? Can you attach the wb?

  8. #8
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    Quote Originally Posted by GTO
    Hi there,

    I am probably overlooking something, but I don't see how this could be? Can you attach the wb?
    I would prefer not to attach the workbook, as it contain personal financial information.

    I attached the code again with my code in the loop, currently I commented out the pasting operations.

    In this state, I just get a copy of the workbook with a sheet# name, with all formating, formulas preserved.

    So the only step that remains is to strip the formulas, otherwise it does what I am trying do to.


    [VBA]Option Explicit
    Sub exa()


    Dim wbNew As Workbook
    Dim wks As Worksheet
    Set wbNew = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Worksheets.Copy After:=wbNew.Worksheets(1)
    Application.DisplayAlerts = False
    wbNew.Worksheets(1).Delete
    Application.DisplayAlerts = True

    For Each wks In wbNew.Worksheets
    Cells.Select
    Selection.Copy
    'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    'Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Next
    End Sub[/VBA]

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Have you got the calculation mode set to manual?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    Quote Originally Posted by xld
    Have you got the calculation mode set to manual?
    Ehm.. where can I check it? :-)

    When I browser through the original workbook, values are calculated after a short delay due to some remote retrieval and references.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try embedding

    [vba]
    Application.CalculateFull
    [/vba]

    before the For Each
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    Quote Originally Posted by xld
    Try embedding

    [vba]
    Application.CalculateFull
    [/vba]
    before the For Each
    no effect at all :-(

  13. #13
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    hmm, if I remove the contains of the For Each loop, i just end up with a copy of the original with formulas and all.

    The funny thing is that for a brief moment when running the macro, it also shows "#VALUE!" in the cells.

    But once finished, values and formulas return...


    this led me to think; am I copying values at the time when it is showing ""VALUE!"???

    Quote Originally Posted by Webmonkey
    Hi!


    This seem to do the loop with end save OK, but all cell which before had numbers, now screem "#VALUE!" at me.

    What went wrong???

  14. #14
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    Ok, problem has been solved!

    It was like Error 65 - a missing "wks" after the "next" in end of loop :-)


    Thanks for all of the input and help I got here!


    btw.

    As said this is my first week of anything VB - please recommend a good site or book with "beginning VBA" :-)

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, that was not the issue. Although it is better to explicitly state the loop element in the Next statement, it is not necessary, and it will not cause the code to fail.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    Quote Originally Posted by xld
    Sorry, that was not the issue. Although it is better to explicitly state the loop element in the Next statement, it is not necessary, and it will not cause the code to fail.
    Ok, this is really strange...

    This is the code I have now:

    [vba]Sub test()

    Dim wbNew As Workbook
    Dim wks As Worksheet
    Set wbNew = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Worksheets.Copy After:=wbNew.Worksheets(1)
    Application.DisplayAlerts = False
    wbNew.Worksheets(1).Delete
    Application.DisplayAlerts = True
    For Each wks In wbNew.Worksheets

    wks.UsedRange.Value = wks.UsedRange.Value

    Next wks
    Application.Dialogs(xlDialogSaveAs).Show
    End Sub[/vba]
    And it works... strange... - could it have been something about indentation of code?? I am used to closing functions with "}" :-)

  17. #17
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    OK, I geuss I spoke to soon...

    After re-loading the source workbook, I now again have the #VALUE!

    Could it be related to the Application.CalculateFull ??

    just before the For Each it does not seem to have some effect...

    Is there any switch to stop a sheet from re-calculating?

  18. #18
    VBAX Regular
    Joined
    Jan 2011
    Location
    Copenhagen
    Posts
    11
    Location
    ok.

    i needed to set the [VBA]Application.Calculation = xlCalculationManual[/VBA]

    At the first line of my code and the corresponding code to set calculation back to auto again once I am done.

    I the problem with #VALUE! was that while waiting for the values to load, it copied the "empty" values and thus I ended up with #VALUE! in my cells.

    now it work!

    Thanks once again guys!

  19. #19
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Remember what I asked you in #9
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •