Excel MVP's - VBA Code Optimization Request

robertgama

Board Regular
Joined
May 4, 2011
Messages
189
I was helping out another forum member with a solution to the following problem:
http://www.mrexcel.com/forum/showthread.php?t=596276

I created a working code solution, but it has to be optimized. Can one of the VBA experts out there make some suggestions to significantly speed up this code for it.

The file can be downloaded here:
https://docs.google.com/open?id=0B1DOEckGTf-SMDhjNzEzYmUtNmEyMS00YmU5LTlkNjQtYzA3ZDJhMDJmYzQw

You can view it running here:
http://www.youtube.com/watch?v=kaaX3MaiKFw

Here is the code:
Code:
Dim runTime

Sub setRunSchedule()
    runTime = Now + TimeValue("00:00:01")
    Application.OnTime runTime, "CheckValue"
End Sub

Sub CheckValue()
    If UCase(Range("stop").Value) <> "Y" Then
        Calculate
        Range("A1").Value = runTime
        Call setRunSchedule
        'comment out line below when your feed is putting the stock price in B3
        Call simulateStockPrice
        Call testValue
    End If
End Sub

Sub simulateStockPrice()
    Dim stockPrice As Long
    Dim priceChange As Double

    Randomize
    priceChange = Application.WorksheetFunction.RandBetween(-14, 14)
    stockPrice = Range("stockPrice").Value + priceChange
    Range("stockPrice").Value = stockPrice
    
End Sub
Sub NewPriceRange(stockPrice As Long)
    Range("openPrice").Offset(-6 + Range("currentRow").Value, 0).Value = stockPrice
    Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value = stockPrice
    Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value = stockPrice
    lowestPrice = stockPrice
    highestPrice = stockPrice
End Sub
Sub testValue()
    Dim lowestPrice As Long
    Dim highestPrice As Long
    Dim curPrice As Long
    Dim openPrice As Long
    Dim priceRange As Integer
    Dim priceInterval As Integer
    Dim closeTime As Date
    Dim changeDirection As Integer
    Dim i As Integer

    Const millisecond As Double = (1# / (24# * 60# * 60# * 1000#))
    
    If Range("openPrice").Offset(-6 + Range("currentRow").Value, 0).Value = "" Then
        Call NewPriceRange(Range("stockPrice").Value)
    End If
    
    closeTime = runTime
    curPrice = Range("stockPrice").Value
    openPrice = Range("openPrice").Offset(-6 + Range("currentRow").Value, 0).Value
    
    lowestPrice = Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value
    highestPrice = Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value
    If curPrice < lowestPrice Then
        lowestPrice = curPrice
    ElseIf curPrice > highestPrice Then
        highestPrice = curPrice
    End If
    priceRange = highestPrice - lowestPrice
    priceInterval = Range("priceInterval").Value
    
    i = 0
    If priceRange > priceInterval Then
        If curPrice = highestPrice Then
            changeDirection = 1
        ElseIf curPrice = lowestPrice Then
            changeDirection = -1
        End If
        Do
            If changeDirection = 1 Then
                highestPrice = lowestPrice + priceInterval
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value = lowestPrice
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value = highestPrice
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 3).Value = priceInterval
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 4).Value = highestPrice
                lowestPrice = highestPrice + 1
                openPrice = lowestPrice
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 5).Value = CDbl(closeTime) + millisecond * 10 * i 'DateAdd("s", i, closeTime)
                priceRange = priceRange - (priceInterval + 1)
            ElseIf changeDirection = -1 Then
                lowestPrice = highestPrice - priceInterval
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value = lowestPrice
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value = highestPrice
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 3).Value = priceInterval
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 4).Value = lowestPrice
                highestPrice = lowestPrice - 1
                openPrice = highestPrice
                Range("openPrice").Offset(-6 + Range("currentRow").Value, 5).Value = DateAdd("s", i, closeTime)
                priceRange = priceRange - (priceInterval + 1)
            End If
            Range("openPrice").Offset(-6 + Range("currentRow").Value, 0).Value = openPrice
            i = i + 1
        Loop Until priceRange < priceInterval
        If changeDirection = 1 Then
            Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value = openPrice
            Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value = openPrice + priceRange
        Else
            Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value = openPrice - priceRange
            Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value = openPrice
        End If
        Range("openPrice").Offset(-6 + Range("currentRow").Value, 3).Value = priceRange
    ElseIf priceRange <= priceInterval Then
        Range("openPrice").Offset(-6 + Range("currentRow").Value, 1).Value = lowestPrice
        Range("openPrice").Offset(-6 + Range("currentRow").Value, 2).Value = highestPrice
        Range("openPrice").Offset(-6 + Range("currentRow").Value, 3).Value = priceRange
    End If
End Sub

Sub ClearValues()
    Range("A6").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    Range("A6").Select
    Range("stop").Value = "n"
End Sub
Thanks,

Rob.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top