Option Explicit
Sub runLoanCBCode()
On Error Resume Next
Dim wb As Workbook, ws As Worksheet
Dim cell As Range, count As Integer
Dim outputLnNumber As String
Dim inputLnNumber As String
Dim rw As String
Dim myPath As String
Dim CBFileSysOutput As String
Dim Response As String
rw = 5
Set wb = Application.Workbooks("Foreclosure Tree.xlsx")
Set ws = Worksheets("Data File").Activate
count = 0
Application.StatusBar = True
CBFileSysOutput = "C:\CBReportingDocLoan2009" & "\" & outputLnNumber & ".cbr"
'need to activate the sheet first
'Create object of Crystal Ball
'Create object of spreadsheet with the loan number
'create object of spreadsheet with the loan numbers for EOF
'Create a directory to save new copy of CB to
myPath = "C:\CBReportingDocLoan2009"
If Dir$(myPath, vbDirectory) = "" Then MkDir myPath
'run the loop
For Each cell In ws.Range("B5", ws.Range("B863").End(xlUp))
'1. pull the loan number from the data file inside of workbook -->set in DB format; all loan #'s
'Get cell data
cell.Value = outputLnNumber
'2. insert the loan number into the cell
Application.Workbooks("Foreclosure Tree.xlsx").Worksheets("Model").Activate
Range("loan_num").Value = outputLnNumber
'3.start the CB, run the addin from here
'Application.Run (Application.Workbooks("Foreclosure Tree.xlsx").Worksheets("Model"))
'Application.Workbooks("Foreclosure Tree.xlsx").Worksheets("Model").Activate = CB.Startup()
Response = MsgBox("Click OK to run Crystal Ball")
'Make sure that we reset the simulation trial back to 1
CB.ResetND
'Now lets the run the simulation after the vlaue has been inputed in the loan_num cell
CB.Simulation 1000
'4.wait until done
If Dir(CBFileSysOutput) = "" Then
DoEvents
Else
Application.Workbooks("Foreclosure Tree.xlsx").Worksheets("Model").Activate = CB.shutdown()
End If
'5. Capture the results in Excel Sheet
'6. Repeat until the end of the file
Application.Workbooks("Foreclosure Tree.xlsx").Worksheets("Model").Activate = CB.shutdown()
Next cell
Response = MsgBox("Task Complete")
End Sub
|