Easier way to record FormulaR1C1 entries from a worksheet?

ronnie76

Board Regular
Joined
Jul 26, 2007
Messages
101
Have a worksheet which contains about unique Excel formulas in over 1,500 different cells. Trying to create a macro which will recreate this worksheet whenever needed. The only way I know how to record all these entries is to double click into each cell then hit enter which then records the FormulaRIC1 entry. Is there an easier way to record all of the cell entries from the source sheet?

Would prefer not to copy and paste if possible, but if that is the best way, then I would need a way not to copy absolute workbook references. Example below.

Copying from one workbook to another this

Code:
=IF(ISBLANK($D13),"",VLOOKUP($D13,'Raw_Material_Data'!$A:$AY,6,FALSE))

becomes this

Code:
=IF(ISBLANK($D13),"",VLOOKUP($D13,'[Formulation Template v3.3 with Create Macro.xlsm]Raw_Material_Data'!$A:$AY,6,FALSE))

Which causes the new workbook to try and reference the source workbook when opening.

Thanks so much for any help or advice.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Can you copy and paste formulas then use Find and Replace (CTRL + h) and replace "[Formulation Template v3.3 with Create Macro.xlsm]" with ""?

That will prevent the formula from referencing an outside worksheet. You'll need to be careful that you have the correct tab names though or you may get some nasty errors.
 
Upvote 0
Try this code, the way it works is it loads the formulae in the active sheet into a variant array and then loops through each cell. When it finds a formula it generates the VBA code to put the formula into the same cell
When it has completed the loop it adds and extra worksheet and writes the code out into column A. Now select all the rows with code in it in column A, and select copy, then open a new subroutine in VBA with no code in it an paste the code into the subroutine. This will replicate the equations in your worksheet exactly as they were.
VBA Code:
Sub form()
Dim outarr() As Variant
tt = Chr(34)
formarr = ActiveSheet.UsedRange.Formula
maxdim = UBound(formarr, 1) * UBound(formarr, 2)
ReDim outarr(1 To maxdim, 1 To 1)
indi = 1
For i = 1 To UBound(formarr, 1)
For j = 1 To UBound(formarr, 2)
   If formarr(i, j) <> "" Then
    outarr(indi, 1) = "cells(" & i & "," & j & ").formula=" & tt & formarr(i, j) & tt
    indi = indi + 1
   End If
Next j
Next i
ActiveWorkbook.Sheets.Add
Range(Cells(1, 1), Cells(indi, 1)) = outarr


End Sub
 
Upvote 0
Can you copy and paste formulas then use Find and Replace (CTRL + h) and replace "[Formulation Template v3.3 with Create Macro.xlsm]" with ""?

That will prevent the formula from referencing an outside worksheet. You'll need to be careful that you have the correct tab names though or you may get some nasty errors.

Thanks so much for the response. Yes, that does work, though it does create significant chance for errors as you pointed out.
 
Upvote 0
Try this code, the way it works is it loads the formulae in the active sheet into a variant array and then loops through each cell. When it finds a formula it generates the VBA code to put the formula into the same cell
When it has completed the loop it adds and extra worksheet and writes the code out into column A. Now select all the rows with code in it in column A, and select copy, then open a new subroutine in VBA with no code in it an paste the code into the subroutine. This will replicate the equations in your worksheet exactly as they were.
VBA Code:
Sub form()
Dim outarr() As Variant
tt = Chr(34)
formarr = ActiveSheet.UsedRange.Formula
maxdim = UBound(formarr, 1) * UBound(formarr, 2)
ReDim outarr(1 To maxdim, 1 To 1)
indi = 1
For i = 1 To UBound(formarr, 1)
For j = 1 To UBound(formarr, 2)
   If formarr(i, j) <> "" Then
    outarr(indi, 1) = "cells(" & i & "," & j & ").formula=" & tt & formarr(i, j) & tt
    indi = indi + 1
   End If
Next j
Next i
ActiveWorkbook.Sheets.Add
Range(Cells(1, 1), Cells(indi, 1)) = outarr


End Sub

Outstanding! Thank you so much for the reply and help!
 
Upvote 0
Try this code, the way it works is it loads the formulae in the active sheet into a variant array and then loops through each cell. When it finds a formula it generates the VBA code to put the formula into the same cell
When it has completed the loop it adds and extra worksheet and writes the code out into column A. Now select all the rows with code in it in column A, and select copy, then open a new subroutine in VBA with no code in it an paste the code into the subroutine. This will replicate the equations in your worksheet exactly as they were.
VBA Code:
Sub form()
Dim outarr() As Variant
tt = Chr(34)
formarr = ActiveSheet.UsedRange.Formula
maxdim = UBound(formarr, 1) * UBound(formarr, 2)
ReDim outarr(1 To maxdim, 1 To 1)
indi = 1
For i = 1 To UBound(formarr, 1)
For j = 1 To UBound(formarr, 2)
   If formarr(i, j) <> "" Then
    outarr(indi, 1) = "cells(" & i & "," & j & ").formula=" & tt & formarr(i, j) & tt
    indi = indi + 1
   End If
Next j
Next i
ActiveWorkbook.Sheets.Add
Range(Cells(1, 1), Cells(indi, 1)) = outarr


End Sub


@offthelip

Thank you so much. The code worked great, but there were just a few errors. Do you happen to know of a way to avoid the compile errors like the ones listed below?


Recording the macro result
VBA Code:
Range("B3").Select
    ActiveCell.FormulaR1C1 = _
        "=REPLACE( CELL(""filename"",R[-2]C[-1]),1, FIND(""]"",CELL(""filename"",R[-2]C[-1])),"""")"

Your extraction code result which results in a Compile Error on "filename"
Code:
cells(3,2).formula="=REPLACE( CELL("filename",A1),1, FIND("]",CELL("filename",A1)),"")"




Recording the macro result
Code:
ActiveCell.FormulaR1C1 = _
        "=IF(RC[-1]="""","""",IF(RC[-1]=""Natural Flavors"",SUMIF(R93C12:R132C12,RC[-1],R93C13:R132C13)-R133C13,SUMIF(R93C12:R132C12,RC[-1],R93C13:R132C13)))"

Your extraction code result which results in a Compile Error on "Natural"
Code:
cells(97,8).formula="=IF(G97="","",IF(G97="Natural Flavors",SUMIF($L$93:$L$132,G97,$M$93:$M$132)-$M$133,SUMIF($L$93:$L$132,G97,$M$93:$M$132)))"
 
Upvote 0
Found what I think is the proper find and replace sequence which will fix the errors, so I can just use that if necessary.

On the ones with the compile errors it would be replace

" with ""
""= with "=
)"" with )"
 
Upvote 0
Sorry ,I forgot to take that into account, it is one of the the reasons I use the constant "tt=chr(34) which is a " . It makes is a bit clearer which double quote is part of the VBA and which is part of the text.
However you hopefully have found the solution.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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