Excel formula to copy/paste formula needed please.

C

colwyn

I use excel 2007 and have a spreadsheet which has 350,000 rows of
data.

The data consists of many ranges.

Each range has a cell containing a formula. These cells are in the
same column but refer to different cells on another sheet.

I want to put the formula on the row above each range.

If I copy/paste I just get the result of the formula. Is it possible
to to have a formula which copy/pastes another formula???
Big thanks.
Colwyn.
 
S

Sheeloo

If you don't have formulae in two consecutive rows then the macro given below
will copy the formula, as text, in the cell above the cell containing the
formula.
Needs to be run only once... Make a copy of your spreadsheet before testing...
To enter the macro
-------------
1. Open the Visual Basic Editor by going to tools-Macro's-Visual Basic
Editor or use Alt-F11
2. On the toolbar of the Visual Basic Editor, go to insert - module after
selecting the workbook on the left in which you want to use this code
3. In the module pane paste the code below.
4. Close the Visual Basic Editor By clicking the X in the upper right
corner or go to File-Close
-------------
'-------------
'Code to copy. copy upto End Sub...
Sub copyFormulaString()
Dim LastRow
LastRow = Sheets("Sheet3").Cells(1048576, "A").End(xlUp).Row
Range("A" & LastRow).Select
Do Until ActiveCell.Row = 1
If Left(ActiveCell.Formula, 1) = "=" Then
ActiveCell.Offset(-1, 0).Value = "'" & ActiveCell.Formula
If ActiveCell.Row > 2 Then
ActiveCell.Offset(-1, 0).Select
End If
End If
ActiveCell.Offset(-1, 0).Select
Loop
End Sub
'-------------
 
C

colwyn

Sheeloo, excellent answer. I asked on several boards but yours was the
only one to do what was wanted.
Just one slight problem. The formula was being COPIED and pasted (my
fault!) when what I wanted it to do was CUT and paste. Now I have the
formula in two cells the original and the new one copied by your
formula in the cell above. Any idea as to how I can do this ??

I should add that I changed the code in order to show the result of
the formula rather than the text of same. That is, where your code
says
ActiveCell.Offset(-1, 0).Value = "'" & ActiveCell.Formula
I have deleted the ' between the " " as this now produces the formula
result.

Thank you for your help'
Colwyn.
 
S

Sheeloo

Glad I could help.

Just add the line
ActiveCell.Value=""
after the changde line
ActiveCell.Offset(-1, 0).Value = "'" & ActiveCell.Formula
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top