Solved

VBA script to convert all formulas in values of all the sheets of the file

Posted on 2014-11-11
3
2,169 Views
Last Modified: 2014-11-12
Excel 2010:
How can I convert all formulas of all the sheets of the file in values, preserving formatting?
I tried by manually copying and pasting values over a whole sheet but it fails because there are many merged cells.

I need that because I used some INDIRECT formulas that require the external file sources to be opened and I cannot send these source files to the recipient...
Excel unfortunately doesn't cache the results of these formulas...
0
Comment
Question by:lucavilla
3 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 400 total points
ID: 40436252
This code will loop through all worksheets in a workbook and convert all formula cells to their value.
Sub Replace_Formulas()
    Dim wbk As Workbook
    Dim ws As Worksheet
    Dim rng As Range
    Dim cl As Object
    
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    On Error Resume Next
    
    Set wbk = ActiveWorkbook
    For Each ws In wbk.Sheets
        Set rng = ws.Range("A1").SpecialCells(xlCellTypeFormulas, 23)
        If Not (rng Is Nothing) Then
            For Each cl In rng
                cl.Value = cl.Value
            Next cl
        End If
    Next ws
    Application.Calculate
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub

Open in new window


Regards,
-Glenn
0
 
LVL 20

Assisted Solution

by:ltlbearand3
ltlbearand3 earned 100 total points
ID: 40436626
Depending on how often you need to accomplish this, you can probably do this with just straight Excel.  I assume you want to remove ALL formulas and replace them with their values.

First select all the sheets in the workbook by click the first sheet, then hold down the shift key and click the last sheet.  Now press Cntrl-A to select all cells on all sheets.  Now copy (Cntrl-C).  Now do a paste special and choose Values.  Then you can save the workbook as a new file that will no longer have any links or formulas.

No code required.
0
 

Author Closing Comment

by:lucavilla
ID: 40436797
Wonderful, thanks!!!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I comeā€¦
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question