Results 1 to 4 of 4

Thread: slow loop! any advice??

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    2

    slow loop! any advice??

    I have a loop which currently runs 96*19*10 = 18,240 times and it's taking a long time (about 5 minutes). Is this to be expected? Anyone have any advice on how to increase efficiency? Thanks!!

    Code:
    Sub simulations()
    
    'set timer
    Dim StartTime As Double
    Dim EndTime As Double
    
    StartTime = Timer
    
    Application.ScreenUpdating = False
        'Select toggle sheet to start
    
    Dim iterations As Integer
    Dim days As Integer
    Dim startdate As Date
    Dim securities As Integer
    
    Dim i As Long
    Dim j As Integer
    Dim d As Long
    
    Dim swap As String
    Dim rows As Integer
    Dim cols As Integer
    
    
    iterations = Range("iterations")
    days = Range("dates").rows.Count - 1
    startdate = DateSerial(2008, 5, 30)
    n = 1
    securities = Range("swaps").Columns.Count
    
    ReDim Output(1 To iterations) As Long
    ReDim Results(1 To securities, 1 To days) As Long
    
    For Each security In Range("swaps")
    
        swap = security
        Range("swap") = security
    
        
        'loop through valuation dates
        For d = 1 To days
        
            Range("ValuationDate") = Range("base").Offset(Application.Match(CLng(startdate), Range("dates")) - 2 + d, 0)
        
                'loop through iterations
                For i = 1 To iterations
            
                    Range("toggle") = i
                
                    Output(i) = Application.WorksheetFunction.Sum(Range("results"))
                  
                Next
            
            On Error Resume Next
            Results(n, d) = Application.WorksheetFunction.Average(Output) / Range("spotvaluationdate")
            Erase Output
            ReDim Output(1 To iterations) As Long
    
        Next
        
        'increment swap number
        n = n + 1
    
    Next
    
        'paste results
        rows = Application.Match(CLng(startdate), Range("dates"), 0) - 1
        Range(Range("base").Offset(rows, 1), _
              Range("base").Offset(rows + d - 2, securities)) = WorksheetFunction.Transpose(Results)
    
    
    
    'display run time
    EndTime = Timer - StartTime
    MsgBox EndTime
    
    
    End Sub
    Last edited by si_the_geek; Aug 17th, 2012 at 11:35 AM. Reason: added Code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width