Consulting

Results 1 to 5 of 5

Thread: Solved: Copying worksheet formula results to a new workbook.

  1. #1
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location

    Solved: Copying worksheet formula results to a new workbook.

    A colleague of mine wants to create a macro, so that when they press a button, the current worksheet generates and saves a new workbook.
    The new workbook must contain the contents of the sheet that was active when they pressed the button that activated the macro.

    However the contents of the new workbook must only contain the results of any formula from the old worksheet, and not the formula itself.

    (Using Activesheet.copy copies the formula).

    Here's the code I have so far, although I think it's a bit messy, as it has been adapted from some other code, and it doesn't allow the user to choose the destination and filename to save the new workbook to:

    [VBA]Sub Copyworksheet()
    'Variable declaration
    Dim oApp As Object, _
    WB As Workbook, _
    FileName As String

    'Turn off screen updating
    Application.ScreenUpdating = False

    'Make a copy of the active sheet and save it to a file
    ActiveSheet.Copy
    Set WB = ActiveWorkbook
    FileName = ActiveSheet.Name & ".xls"
    On Error Resume Next
    Kill "C:\Documents and Settings\All Users\Desktop\" & FileName
    On Error GoTo 0
    WB.SaveAs FileName:="C:\Documents and Settings\All Users\Desktop\" & FileName

    Application.ScreenUpdating = True
    Set oApp = Nothing

    End Sub[/VBA]

    Thanks in advance.

  2. #2
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    If refined the code I had something rotten:

    [VBA]Sub Copyworksheet()
    ActiveSheet.Copy

    ChDir ThisWorkbook.Path

    Application.Dialogs(xlDialogSaveAs).Show

    If Not ActiveWorkbook.Name = ThisWorkbook.Name _
    Then ActiveWorkbook.Close False

    End Sub
    [/VBA]

    Which is much neater. However the Copied sheet still includes all formula, and not the results of the formula.

    Any ideas?

    Cheers

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    You may want to do this to the new workbook to lose the formulae.

    [VBA] Cells.Copy
    Cells.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False[/VBA]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    [VBA]Sub Copyworksheet()
    ActiveSheet.Copy

    ChDir ThisWorkbook.Path

    Application.Dialogs(xlDialogSaveAs).Show

    ActiveSheet.Cells.Copy
    ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

    If Not ActiveWorkbook.Name = ThisWorkbook.Name _
    Then ActiveWorkbook.Close True

    End Sub[/VBA]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Great thanks

Posting Permissions

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