Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: How to create a values only copy of WB while keeping formatting and filters in tact?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    How to create a values only copy of WB while keeping formatting and filters in tact?

    Good Day All!

    I am new to the forum on a recommend from a colleague. I have not found anything that fits my particular issue. I have used the code below to create the values only copy of the workbook. The issue is the copy does not keep the formatting nor the filters in tact. I'm not sure if this is possible but any suggestions would be appreciated.

    Thanks for your time.

    Sub CreateValuesOnly()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim Output As Workbook, Source As Workbook
    Dim sh As Worksheet
    Dim FileName As String
        FileName = "C:\Users\blittlej\Desktop\ValuesOnly_.xlsx"
    Dim firstCell
    Dim curdate As String
        curdate = Format(Now(), "yyyy-MM-dd")
    Set Source = ActiveWorkbook
    Set Output = Workbooks.Add
    Output.SaveAs FileName
    Dim i As Integer
    For Each sh In Source.Worksheets
       Dim newSheet As Worksheet
    ' select all used cells in the source sheet:
       sh.Activate
       sh.UsedRange.Select
       Application.CutCopyMode = False
       Selection.Copy
    ' create new destination sheet:
      Set newSheet = Output.Worksheets.Add(after:=Output.Worksheets(Output.Worksheets.Count))
      newSheet.Name = sh.Name
    ' make sure the destination sheet is selected with the right cell:
      newSheet.Activate
      firstCell = sh.UsedRange.Cells(1, 1).Address
      newSheet.Range(firstCell).Select
    ' paste the values:
      Range(firstCell).PasteSpecial Paste:=xlPasteValues
      Range(firstCell).PasteSpecial Paste:=xlPasteFormats
    'Range(firstCell).PasteSpecial Paste:=xlPasteColumnWidths, _
    'Operation:=xlNone, SkipBlanks:=True, Transpose:=False
    Next
    ' delete the sheets that were originally there
    While Output.Sheets.Count > Source.Worksheets.Count
       Output.Sheets(1).Delete
    Wend
    FileName = "C:\Users\blittlej\Desktop\ValuesOnly_" & curdate & ".xlsx"
    Output.SaveAs FileName
    Output.Close
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    Last edited by Aussiebear; 08-09-2022 at 03:47 PM. Reason: Reduce the whitespace

  2. #2
    Well, I figured out that if I change
    Range(firstCell).PasteSpecial Paste:=xlPasteFormats
    with
    Range(firstCell).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone , SkipBlanks:=False, Transpose:=False
    I get the formats and filters. However, formulas copy over as well and I only want values. Any thoughts on this?

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    All your code can be replaced by:

    Sub M_snb()
        Application.ScreenUpdating = False
        
        ThisWorkbook.Sheets(1).Copy
        With ActiveWorkbook
           .Sheets(1).UsedRange.Value = .Sheets(1).UsedRange.value
           .SaveAs "C:\Users\blittlej\Desktop\ValuesOnly_.xlsx", 51
           .Close 0
        End With
    End Sub
    Last edited by snb; 08-10-2022 at 07:38 AM.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    @snb. What does this line represent ".Sheets(1).UsedRange.Value = .Sheets(1).UsedRange.value"?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    It turns formulae into values.

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Doesn't the very next line do that when saving?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    No, it doesn't turn formulae into values, it only saves the file by a certain name and an indicated format (fileformat; .xlsx =51; .xlsb= 50; etc.)

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Isn't it saving as values at the same time
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Not in my Excel version (2010).
    And I hope not in other versions too. How would you otherwise be able to store a file with formulae ?

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    We are walking in circles here. I draw your attention back to my initial question
    what does this line represent .Sheets(1).UsedRange.Value = .Sheets(1).UsedRange.value
    How does this turn formula into values?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Just run the code.

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Bullshit son.. tell me how it works.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    My understanding (and I learned it here) ....

    When you set the .Value of a destination cell (even if it contains a formula), any formula or value in that cell is replaced with a computed value (not a formula)

    So if

    Range("A1").Formula = "=10*B1"
    Range("B1").Value = 100

    then

    Range("A1").Value = Range("A1").Value

    is sort of like a Copy/Paste Values from and onto itself in that the .Value is replaced by the results of the formula, giving

    Range("A1").Value = 1000

    Range("A1").Formula = 1000 also (no computation)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  14. #14
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Thank you Paul. We used to see a lot of the Range('A1").formula = Range("A1").value type constructions where it was understood that .formula was being copied and pasted as .value. Where it poses an issue is when we are taking a .value and copy/paste as a .value. If its already a value why convert it to a value?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  15. #15
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Quote Originally Posted by Aussiebear View Post
    Thank you Paul. We used to see a lot of the Range('A1").formula = Range("A1").value type constructions where it was understood that .formula was being copied and pasted as .value. Where it poses an issue is when we are taking a .value and copy/paste as a .value. If its already a value why convert it to a value?
    The cell may or may not be a formula but either way it has a value, it is taking that value and overwriting the formula with said value. So taknig the value of a calculated formula and placing the value there instead.

    Value of [A1] = the value/ result of [A1]
    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 (you may need to click 'Go Advanced')

    Excel 365, Version 2403, Build 17425.20146

  16. #16
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by georgiboy View Post
    The cell may or may not be a formula but either way it has a value.
    In the formula bar Excel displays it as a formula rather than a value unless you specifically force Excel to show the value. To do this Excel must be giving a priority to the formula rather than a value but in VBA it seems therefore that it is the other way around.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  17. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by Aussiebear View Post
    In the formula bar Excel displays it as a formula rather than a value unless you specifically force Excel to show the value. To do this Excel must be giving a priority to the formula rather than a value but in VBA it seems therefore that it is the other way around.

    When you select a cell that contains a formula (.Formula = "=123+123"), then =123+123 is displayed in the Formula Bar

    If you then click in the Formula Bar, and press F9, you see the results (aka .Value) for that cell, 246

    So they're both really always there
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  18. #18
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by Aussiebear View Post
    Thank you Paul. We used to see a lot of the Range('A1").formula = Range("A1").value type constructions where it was understood that .formula was being copied and pasted as .value. Where it poses an issue is when we are taking a .value and copy/paste as a .value. If its already a value why convert it to a value?
    Not all .Value are created equal

    .Formula cells also have a .Value = results of the .Formula

    Entering a .Value replaces any existing .Value and .Formula. The new 'formula' is just the value


    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  19. #19
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Now I'm wondering why snb couldn't simply explain this.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  20. #20
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    VBA basics

    Sub M_snb()
      Cells(1, 1) = 100
      Cells(2, 1) = 200
      Cells(3, 1) = "=A1+A2"
       
      Debug.Print 1, Cells(3, 1).Formula
      Debug.Print 2, Cells(3, 1).Value
      Debug.Print 3, Cells(3, 1).Text
       
      Cells(3, 1) = Cells(3, 1).Value
      
      Debug.Print 4, Cells(3, 1).Formula
      Debug.Print 5, Cells(3, 1).Value
      Debug.Print 6, Cells(3, 1).Text
       
      Cells(3, 1).NumberFormat = "€ 0000"
       
      Debug.Print 7, Cells(3, 1).Formula
      Debug.Print 8, Cells(3, 1).Value
      Debug.Print 9, Cells(3, 1).Text
    End Sub
    You could have seen the result when running the code.
    No helper is obliged to do anything. Laziness shouldn't be rewarded.
    Why can't you apologize for your rude behaviour ?
    A moderator is obliged to respect and apply the courtesy rules in a forum.

Posting Permissions

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