Автоматический перезапуск VBA
Я создаю программу автоматического перезапуска VBA Excel. Когда условие выполнено (например, "L"), эта программа попытается перезапустить и выполнить противоположное условие (например, "S")
Итак, что я хочу создать, это: если я начну с L выполнить L ==> restart ==> выполнить S ==> restart ==> выполнить L ==> restart......... и аналогично, если я начинаю с S выполнить S ==> перезапустить ==> выполнить L ==> перезапустить ==> выполнить 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").Range("U6"), он входит в findClosestMarketPrice() и успешно возвращает "s" и вызывает StartAndStop.Start 'restart
Но после "Call StartAndStop.Start", когда он начинается с S и currentMarketPrice >= ThisWorkbook.Sheets("TradingPage").Range("U6"), он автоматически переходит в findClosestMarketPrice() дважды. Как: если я начну с L выполнить L ==> перезапустить ==> выполнить S ==> перезапустить ==> выполнить L ==> выполнить L ==> перезапустить ==> выполнить S ==> выполнить S ==> перезапустить......... и аналогично, если я начну с S, выполните S ==> restart ==> выполните L ==> restart ==> выполните S ==> выполните S ==> restart ==> выполните L ==> выполнить L ==> перезапустить.........
и это неправильно!
Как я могу создать программу, как это: если я начинаю с L выполнить L ==> restart ==> выполнить S ==> restart ==> выполнить L ==> перезапустить......... и аналогично, если я начинаю с S выполнить S ==> перезапустить ==> выполнить L ==> перезапустить ==> выполнить S ==> перезапустить.........
1 ответ
Создайте логическую переменную для отслеживания состояния расписания и передачи следующего состояния макросу reopen в качестве параметра.
Замечания: TimeSerial
принимает только целые числа в качестве параметров
Public ScheduleS As Boolean
Sub ReStart() 'method inside here will be looping until stop
Application.OnTime Now + 0.000001, "'ReOpen" & Chr(34) & (Not ScheduleS) & Chr(34) & "'"
ThisWorkbook.Close SaveChanges:=True
End Sub
Sub ReOpen(bSchedule As Boolean)
ScheduleS = bSchedule
MsgBox ScheduleS
End Sub