Monday 5 December 2011

Monte Carlo Introduction - VBA code

'Written by Satendra Tiwari. Mail me at smani7dm2000@yahoo.co.in
' Refer here for the monte Carlo method
' http://www.chem.unl.edu/zeng/joy/mclab/mcintro.html

' MC Intro to calculate the value of Pi



Sub MonteCarlopi()
Dim numsims, count As Long
Dim xcord, ycord, dist, pi As Double
'Choose the number of simulations
numsims = 1000000
count = 0
For i = 1 To numsims
'Create random point in xy plane. Generate random x and y co-ordinates
xcord = 2 * Rnd() - 1
ycord = 2 * Rnd() - 1
'Calculate the distance of this point
dist = Sqr(xcord * xcord + ycord * ycord)
If dist <= 1 Then
count = count + 1
End If
Next i
'Calcualte the value of pi
pi = 4 * (count / numsims)
MsgBox "Value of pi is " + CStr(pi)

End Sub

Compare two similar database tables (VBA code)

(1) Create two user DSNs on two tables of the 2 databases or use the same DSN for two table queries
(2) Specify the table queries in cell B1 and B2
(3) Specify the name of the worksheets. These tables would be automatically filled if compare is ruing in mode 1(see CompareFunctionSetup)
(4) If compare is runnning in mode 2 then manually create two work sheets and fill them with the data to be compared.

Download Excel File(with VBA code) from here :- http://www.4shared.com/document/S61AlXme/DBTableCompareUtil.html