Hi Fred,
Corrections, the short macro (macro33) was invalid
a new macro is supplied below faster than the longer
one previously included.
This is what copy paste formulas does:
Copy, special formulas copies what is seen
on the formula bar (both formulas and constants) as
opposed to values (both formulas and constants).
So this new version of the macro processes
64 rows at a time if there are more than 8192 ranges
(since this can't be tested, the actual test will be to
test for more than 1 areas in the range).
http://support.microsoft.com/?kbid=832293
Option Explicit
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Sub CopyFormulasFromMaster()
'D.McRitchie, excel.misc, 2004-05-20 revised for speed
' For the sake of speed Copy Range has been used
' but it will be copying both formulas and formatting
' for those cells that have formulas.
Dim Rng As Range, i As Long, cell As Range, area As Range
Dim msg As String, x As Long, j As Long
Dim masterN As String, msgDescr As String
masterN = InputBox("Supply name of 'Master' worksheet", _
"Get Formulas from Master", "Master")
If masterN = "" Then
MsgBox "exiting for by your command, no master sheetname supplied"
Exit Sub
End If
'quick attempt to make sure lastcell position is correct in master
x = Sheets(masterN).UsedRange.Rows.Count
On Error Resume Next
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim myTime(4) As Double, formulasCnt As Long, areasCnt As Long
myTime(1) = timeGetTime()
Dim subArea As Range
Set Rng = Sheets(masterN).Cells.SpecialCells(xlFormulas)
On Error GoTo 0
If Rng.Areas.Count > 1 Then 'okay for fast track
For j = 1 To Rng.Areas.Count
'Warning: -- COPY will copy formula, and formatting
Rng.Areas.Item(j).Copy _
Destination:=ActiveSheet.Range(Rng.Areas.Item(j).Address)
Next j
areasCnt = Rng.Areas.Count
formulasCnt = Rng.Count 'everything in range is a formula
msgDescr = "Fast Track was used on " & Rng.Count _
& " formulas, in " & Rng.Areas.Count & " areas"
GoTo done
End If
msgDescr = "More than 8192 formula areas -- slow track was used"
On Error Resume Next
For i = 1 To x Step 64 'and use that count for the loop
Err.Number = 0
Set Rng = Sheets(masterN).Rows(i & ":" & i + 63).SpecialCells(xlFormulas)
If Err.Number <> 0 Then GoTo next_i
For j = 1 To Rng.Areas.Count
'Warning: -- COPY will copy formula, and formatting
Rng.Areas.Item(j).Copy _
Destination:=ActiveSheet.Range(Rng.Areas.Item(j).Address)
'Rng.Areas.Item(j).Interior.ColorIndex = 39
Next j
formulasCnt = formulasCnt + Rng.Count
areasCnt = areasCnt + Rng.Areas.Count
' '-- if you don't want to copy formatting don't use areas
' '-- but it may take a lot longer
' For Each cell In Rng
' Range(cell.Address).Formula = cell.Formula
' formulasCnt = formulasCnt + 1
' Next cell
next_i:
Next i
done:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
myTime(2) = timeGetTime()
myTime(3) = (myTime(2) - myTime(1)) / 1000
myTime(4) = x / myTime(3)
MsgBox msgDescr & Chr(10) _
& areasCnt & " Formula areas" & Chr(10) _
& Format(myTime(3), "#.000") _
& " seconds, for " & x & " rows, at " _
& Format(myTime(4), "#.0") & " rows per second" _
& Chr(10) & formulasCnt & " formulas at " & _
Format(formulasCnt / myTime(3), "0.0") & " per second"
End Sub
My results from testing on a 600mHz machine Excel 2000
Fast Track was used on 30170 formulas, in 10 areas
112.522 seconds, for 3017 rows, at 26.8 rows per second
30170 formulas at 268.1 per second
Fast Track was used on 6034 formulas, in 2 areas
22.232 seconds, for 3017 rows, at 135.7 rows per second
6034 formulas at 271.4 per second
More than 8192 formula areas -- slow track was used
13628 Formula areas
88.006 seconds, for 3017 rows, at 34.3 rows per second
29264 formulas at 332.5 per second
More than 8192 formula areas -- slow track was used
13629 Formula areas
31.826 seconds, for 3017 rows, at 94.8 rows per second
29263 formulas at 919.5 per second
So it really is going to be how many formulas you
have to copy, whether you have 5 areas,
or 13578 areas. If I did 64 rows at a time
(8192 / 256 * 2) the results were still about the same.
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages:
http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page:
http://www.mvps.org/dmcritchie/excel/search.htm