Anda di halaman 1dari 5

# excel-vba.

com
1 million calculations

Sometimes there are too many formulas in a workbook and it takes an eternity to run. It is time to use the big weapons the
variable of the VARIANT type..

The VBA procedure within this workbook enter 50,000 numbers chosen at random between 1 and 235 in column A of the
sheet "Results" and then executes 1,000,000 calculations in less than 5 seconds following the calculation rules presented
on the sheet "Calculation Rules".

I have used this approach to validate the format of millions of data that the Chase Manhattan Bank wanted to transfer from
one database to another.

Excel tutorials and a printable
book for
\$49.00
the big weapons the

5 in column A of the
ation rules presented

## anted to transfer from

The VBA code
Sub proVariant() Excel tutorials and a printable
book for
' www.excel-vba.com \$49.00
' Peter

' First declare a variable of the type variant in which will be loaded the table
Dim varData As Variant
' A counter
Dim varCounter As Double

' I create the VARIANT variable including 21 columns and 50,000 rows

shResults.Select
varData = shResults.Range("A1", Range("A1").Offset(49999, 20))

## 'Executing the calculations

' In column A a number between 1 and 235 is chosen at random
and the calculations are executed in the next 20 columns
For varCounter = 1 To 50000
varData(varCounter, 1) = Int(Rnd() * 234) + 1
varData(varCounter, 2) = varData(varCounter, 1) * 3
varData(varCounter, 3) = varData(varCounter, 2) / 6
varData(varCounter, 4) = varData(varCounter, 3) * 10
varData(varCounter, 5) = varData(varCounter, 4) + varData(varCounter, 2)
varData(varCounter, 6) = varData(varCounter, 5) + 1000
varData(varCounter, 7) = varData(varCounter, 6) * 7
varData(varCounter, 8) = varData(varCounter, 7) / 2
varData(varCounter, 9) = varData(varCounter, 8) + varData(varCounter, 7)
varData(varCounter, 10) = varData(varCounter, 9) / 3
varData(varCounter, 11) = varData(varCounter, 10) * 2
varData(varCounter, 12) = varData(varCounter, 11) * 6
varData(varCounter, 13) = varData(varCounter, 12) + 345
varData(varCounter, 14) = varData(varCounter, 13) / 2
varData(varCounter, 15) = varData(varCounter, 14) - 276
varData(varCounter, 16) = varData(varCounter, 15) * 5
varData(varCounter, 17) = varData(varCounter, 16) - 876
varData(varCounter, 18) = varData(varCounter, 7) * -1
varData(varCounter, 19) = varData(varCounter, 18) * 34
varData(varCounter, 20) = varData(varCounter, 19) * 2
varData(varCounter, 21) = varData(varCounter, 20) / varData(varCounter, 19)

Next

## shResults.Range("A1", Range("A1").Offset(49999, 20)) = varData

Range("A1").Select
End Sub
Calculation Rules
Applied to 50,000 rows these calculations are executed.

## Number of calculations: 50,000 * 20 = 1,000,000

Column B A*3
Column C B/6 Excel tutorials and a printable
Column D C*10 book for
Column E C+D \$49.00
Column G F*7
Column H G/2
Column I G+H
Column J I/3
Column K J*2
Column L K*6
Column M L+345
Column N M/2
Column O N-276
Column P O*5
Column Q P-876
Column R -Q
Column S R*34
Column T S*2
Column U T/S