我正在創建一個自動重啓的VBA excel程序。 當條件滿足時(例如「L」),該程序將嘗試重新啓動並嘗試滿足相反的條件(例如「S」)VBA自動重啓
所以,我想要創建的是: 如果我開始於L 滿足L ==>重新啓動==>履行S ==>重新啓動==>履行L ==>重新啓動......... 和類似的 如果我以S開頭 滿足S = =>重啓==>滿足大號==>重啓==>滿足小號==>重啓.........
我有以下代碼:
Sub Start() 'start the program
continue = True
Call SPTrader.setBasicValue
Call auto_open
End Sub
Sub auto_open() 'only use this will auto open
If (continue = True) Then
Call ScheduleStartProgram
End If
End Sub
Private Sub ScheduleStartProgram() 'method inside here will be looping until stop
t = 6/10 'set time to 0.6s
TimeToRun = now() + TimeSerial(0, 0, t)
Call DayTrade.findCurrenyClosestValue
End Sub
Sub findCurrenyClosestValue() 'dynamic find the closest value
Call findClosestMarketPrice
End Sub
Private Sub findClosestMarketPrice() 'find next price
currentMarketPrice = ThisWorkbook.Sheets("TradingPage").Cells(6, 11).Value 'set Market Price
If (ThisWorkbook.Sheets("TradingPage").Range("U6") <> 0 And currentMarketPrice >= ThisWorkbook.Sheets("TradingPage").Range("U6") And ThisWorkbook.Sheets("TradingPage").Range("U5").Text = "S" Or ThisWorkbook.Sheets("TradingPage").Range("U5").Text = "s" And ThisWorkbook.Sheets("TradingPage").Range("U6") <> 0 And currentMarketPrice >= ThisWorkbook.Sheets("TradingPage").Range("U6")) Then 'test stop poin
Call TimeOut.TimeOut(3) 'time out 3s
ThisWorkbook.Sheets("TradingPage").Range("S3") = ThisWorkbook.Sheets("TradingPage").Range("U6").Text
ThisWorkbook.Sheets("TradingPage").Range("U5") = "L"
ThisWorkbook.Sheets("SP trader").Range("C5") = 0 'set amount=0
ThisWorkbook.Sheets("SP trader").Range("C6") = 0 'set amount=0
ThisWorkbook.Sheets("TradingPage").Range("U6") = 0
Call StartAndStop.auto_close
'End
Call StartAndStop.Start 'restart
Exit Sub
ElseIf (ThisWorkbook.Sheets("TradingPage").Range("U6") <> 0 And currentMarketPrice <= ThisWorkbook.Sheets("TradingPage").Range("U6") And ThisWorkbook.Sheets("TradingPage").Range("U5").Text = "L" Or ThisWorkbook.Sheets("TradingPage").Range("U5").Text = "l" And ThisWorkbook.Sheets("TradingPage").Range("U6") <> 0 And currentMarketPrice <= ThisWorkbook.Sheets("TradingPage").Range("U6")) Then 'test stop point
Call TimeOut.TimeOut(3) 'time out 3s
ThisWorkbook.Sheets("TradingPage").Range("S3") = ThisWorkbook.Sheets("TradingPage").Range("U6").Text
ThisWorkbook.Sheets("TradingPage").Range("U5") = "S"
ThisWorkbook.Sheets("SP trader").Range("C5") = 0 'set amount=0
ThisWorkbook.Sheets("SP trader").Range("C6") = 0 'set amount=0
ThisWorkbook.Sheets("TradingPage").Range("U6") = 0
Call StartAndStop.auto_close
Call StartAndStop.Start 'restart
Exit Sub
Else 'mainly run here
If (currentMarketPrice >= currentClosestPrice And ThisWorkbook.Sheets("TradingPage").Range("U5") = "L" Or ThisWorkbook.Sheets("TradingPage").Range("U5") = "l" And currentMarketPrice >= currentClosestPrice) Then
nextPriceYPosition = nextPriceYPosition - 1
nextPriceXPosition = 17
currentClosestPrice = ThisWorkbook.Sheets("TradingPage").Cells(nextPriceYPosition, nextPriceXPosition)
If (stopPointMethodPosition = 1 And counter = 0) Then ' in order to ensure the stopPointMethodPosition no equal 0
stopPointMethodPosition = 0
counter = counter + 1
End If
stopPointMethodPosition = stopPointMethodPosition + 1
End If
If (currentMarketPrice <= currentClosestPrice And ThisWorkbook.Sheets("TradingPage").Range("U5") = "S" Or ThisWorkbook.Sheets("TradingPage").Range("U5") = "s" And currentMarketPrice <= currentClosestPrice) Then
nextPriceYPosition = nextPriceYPosition + 1
If nextPriceYPosition = 40 Then ' if next price at the boundary, show message
Call StartAndStop.auto_close
End
End If
nextPriceXPosition = 17
currentClosestPrice = ThisWorkbook.Sheets("TradingPage").Cells(nextPriceYPosition, nextPriceXPosition)
If (stopPointMethodPosition = 1 And counter = 0) Then ' in order to ensure the stopPointMethodPosition no equal 0
stopPointMethodPosition = 0
counter = counter + 1
End If
stopPointMethodPosition = stopPointMethodPosition + 1
End If
If (ThisWorkbook.Sheets("TradingPage").Range("U5").Value <> "NA") Then 'for safety
Call FindMethodPosition.runAllFindMethodPosition
End If
If (ThisWorkbook.Sheets("TradingPage").Range("U5").Value = "L" Or ThisWorkbook.Sheets("TradingPage").Range("U5").Value = "l") Then
ThisWorkbook.Sheets("TradingPage").Cells(nextPriceYPosition + 1, nextPriceXPosition).Font.Color = RGB(0, 0, 255) 'set now price blue
ElseIf (ThisWorkbook.Sheets("TradingPage").Range("U5").Value = "S" Or ThisWorkbook.Sheets("TradingPage").Range("U5").Value = "s") Then
ThisWorkbook.Sheets("TradingPage").Cells(nextPriceYPosition - 1, nextPriceXPosition).Font.Color = RGB(0, 0, 255) 'set now price blue
End If
End If
End Sub
例如,當我開始與L和currentMarketPrice < = ThisWorkbook.Sheets(「TradingPage」)。範圍(「U6」),它進入findClosestMarketPrice()併成功返回「s」和Call StartAndStop.Start'重新啓動
但是,當它以S開頭且「Current StartPrice>> ThisWorkbook.Sheets(」TradingPage「)。」範圍「(」U6「) 時,它會自動進入findClosestMarketPrice()兩次。 贊: 如果我以L開頭 滿足L ==>重啓==>履行S ==>重啓==>履行L ==>履行L ==>重啓==>履行S ==>履行S = =>重啓......... 和類似 如果我以S開頭 滿足S ==>重啓==>履行L ==>重啓==>履行S ==>履行S ==>重啓==>履行L ==>履行L ==>重啓.........
這是錯誤的!
如何創建程序是這樣的: 如果我以L 開始履行大號==>重啓==>履行小號==>重啓==>履行大號==>重新開始...... ... 如果我帶S 履行小號==>重啓==>履行大號==>重啓==>履行小號==>重新啓動類似 .........