VBA自动重启

时间:2016-11-30 03:08:20

标签: excel vba restart autorest

我正在创建一个自动重启VBA excel程序。 当条件满足时(例如" L"),该程序将尝试重新启动并尝试满足相反的条件(例如," S")

所以,我想要创建的是: 如果我从L开始     履行L ==>重启==>履行S ==>重启==>履行L ==>重启......... 和类似的 如果我从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&lt; = ThisWorkbook.Sheets(&#34; TradingPage&#34;)。Range(&#34; U6&#34;)开始时,它会进入findClosestMarketPrice()并成功回归&#34; s&#34;并致电StartAndStop.Start&#39;重启

但是&#34; Call StartAndStop.Start&#34;当它以S和currentMarketPrice开始&gt; = ThisWorkbook.Sheets(&#34; TradingPage&#34;)。范​​围(&#34; U6&#34;) 它会自动进入findClosestMarketPrice()两次。 喜欢: 如果我从L开始     履行L ==&gt;重启==&gt;履行S ==&gt;重启==&gt;履行L ==&gt;履行L ==&gt;重启==&gt;履行S ==&gt;履行S ==&gt;重启......... 和类似的 如果我从S开始     履行S ==&gt;重启==&gt;履行L ==&gt;重启==&gt;履行S ==&gt;履行S ==&gt;重启==&gt;履行L ==&gt;履行L ==&gt;重启.........

这是错的!

我该如何创建这样的程序: 如果我从L开始     履行L ==&gt;重启==&gt;履行S ==&gt;重启==&gt;履行L ==&gt;重启......... 和类似的 如果我从S开始     履行S ==&gt;重启==&gt;履行L ==&gt;重启==&gt;完成S ==&gt;重新启动.........

1 个答案:

答案 0 :(得分:0)

创建一个布尔变量来跟踪调度的状态,并将下一个状态作为参数传递给重新打开的宏。

注意: TimeSerial仅将整数作为参数

enter image description here

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