restoring original formulas

Mr_Phil

Board Regular
Joined
May 28, 2018
Messages
142
Office Version
  1. 365
I feel I should know this but I am drawing a blank. I have a worksheet with a grid that is populated by a formula in each cell. But sometimes users won't like the result and will type over the formula to get the number they want. I've showed them how to copy down and across to put the formula back, but most forget to do it. Is there a way to create a macro to restore the formulas to the entire grid?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You could create a macro to write the formulas back to the range.
 
Upvote 0
Insert a helper sheet.
Copy-paste the range formula to the same range on the helper sheet.
Write a macro to copy back the formula from the helper sheet.
 
Upvote 0
Insert a helper sheet.
Copy-paste the range formula to the same range on the helper sheet.
Write a macro to copy back the formula from the helper sheet.
I like it. I have a helper sheet already that has a lot of scattered pivot tables etc that provide aggregates to other areas of the workbook.
Is there a way to copy the formulas so that it doesn’t change the cell references automatically or is that why we’d do it to the same range?

I did copy the range using find/replace first to change the equal sign to “zebra”. But, when I pasted it back to the working sheet it messed with the formatting even after find/replace to “restore” the formulas. Your answer tells me I am not crazy and that there’s merit to what I was thinking. Thanks for the insight.
 
Upvote 0
Is there a way to copy the formulas so that it doesn’t change the cell references automatically or is that why we’d do it to the same range?

I did copy the range using find/replace first to change the equal sign to “zebra”. But, when I pasted it back to the working sheet it messed with the formatting even after find/replace to “restore” the formulas.
Try something like this:
In this example the formula are in Sheet1 cells B2:C2 & C6:D6.
Select range B2:D6 then run "Sub export_formula"
Book1
ABCD
1N
2UNUNU
3D
4WRyanRonaldDonna
5B
6GBGQS
7Q
8S
Sheet1
Cell Formulas
RangeFormula
B2:C2B2=A1&A2
C6C6=A5&A6
D6D6=A7&A8

The result in sheet Helper is like this (note: sheet Helper must be a clean sheet):
Book1
ABCD
1
2A1&A2B1&B2
3
4
5
6A5&A6A7&A8
7
helper

To send the formulas back to Sheet1:
Select the range in sheet Helper
Run Sub import_formula

If the formulas are in a fixed range then you can replace "Selection" in the macro with the range, so you don't need to select the the range before running the macro.

VBA Code:
Sub export_formula()
Dim c As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
    For Each c In Selection.SpecialCells(xlCellTypeFormulas)
      Sheets("Helper").Range(c.Address) = Mid(c.Formula, 2)
    Next
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub


Sub import_formula()
Dim c As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlManual

For Each c In Selection.SpecialCells(xlCellTypeConstants)
    With Sheets("Sheet1")
        If Not .Range(c.Address).HasFormula Then
            .Range(c.Address) = "=" & c
        End If
    End With
Next

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic
End Sub
 
Upvote 1
Solution
Try something like this:
In this example the formula are in Sheet1 cells B2:C2 & C6:D6.
Select range B2:D6 then run "Sub export_formula"
Book1
ABCD
1N
2UNUNU
3D
4WRyanRonaldDonna
5B
6GBGQS
7Q
8S
Sheet1
Cell Formulas
RangeFormula
B2:C2B2=A1&A2
C6C6=A5&A6
D6D6=A7&A8

The result in sheet Helper is like this (note: sheet Helper must be a clean sheet):
Book1
ABCD
1
2A1&A2B1&B2
3
4
5
6A5&A6A7&A8
7
helper

To send the formulas back to Sheet1:
Select the range in sheet Helper
Run Sub import_formula

If the formulas are in a fixed range then you can replace "Selection" in the macro with the range, so you don't need to select the the range before running the macro.

VBA Code:
Sub export_formula()
Dim c As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
    For Each c In Selection.SpecialCells(xlCellTypeFormulas)
      Sheets("Helper").Range(c.Address) = Mid(c.Formula, 2)
    Next
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub


Sub import_formula()
Dim c As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlManual

For Each c In Selection.SpecialCells(xlCellTypeConstants)
    With Sheets("Sheet1")
        If Not .Range(c.Address).HasFormula Then
            .Range(c.Address) = "=" & c
        End If
    End With
Next

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic
End Sub
I think that will do it. Thank you so much for your help.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,396
Messages
6,124,685
Members
449,179
Latest member
kfhw720

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top