-1

我正在创建一个自动重启 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==>restart==> 完成 S==>restart==> 完成 L==>fulfill L==>restart==>fulfill S==>fulfill S==>restart ......... 同样如果我从 S 开始完成 S==>restart==> 完成 L==>restart==> 完成 S==>fulfill S==>restart==>fulfill L ==>履行L==>重启…………

这是错误的!

我怎样才能创建这样的程序:如果我从 L 开始,完成 L==>restart==> 完成 S==>restart==> 完成 L==>restart......... 同样如果我从 S 开始完成 S==>重新启动==> 完成 L==>重新启动==> 完成 S==>重新启动.........

4

1 回答 1

0

创建一个布尔变量以跟踪状态,如果计划并将下一个状态作为参数传递给重新打开宏。

注意: 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
于 2016-11-30T03:41:52.800 回答