2015-05-24 79 views
0

我有这个代码描绘了(B;5)单元格红色,并开始移动它来回移动。Excel VBA同时发布多个宏

Declare Sub Sleep Lib "kernel32" (ByVal dwMillisecons As Long) 

Private Sub Button1_Click() 
Move 
End Sub 

Sub Move() 
gr = 1 
st = 1 
While Cells(2, 2) = 0 
If st > 1 Then 
    Cells(5, st - 1).Clear 
    End If 
Cells(5, st + 1).Clear 
Cells(5, st).Interior.Color = vbGreen 
st = st + gr 
If st > 48 Then 
gr = -1 
End If 
If st < 2 Then 
gr = 1 
End If 
Sleep 100 
DoEvents 
Wend 
End Sub 

如何使这将油漆(B;7)(B,9)细胞,也将开始在同一时间移动它们?

+0

什么是你的代码吗? –

+0

它以绿色绘制细胞,并开​​始来回移动。 – Faux

回答

2

您的代码

If st > 1 Then Cells(5, st - 1).Clear 
Cells(5, st + 1).Clear 
Cells(5, st).Interior.Color = vbGreen 

照顾5.行的简单的用于图7和9

Sub Move() 
    gr = 1 
    st = 1 

    While Cells(2, 2) = 0 
     If st > 1 Then Cells(5, st - 1).Clear 
     Cells(5, st + 1).Clear 
     Cells(5, st).Interior.Color = vbGreen 

     If st > 1 Then Cells(7, st - 1).Clear 
     Cells(7, st + 1).Clear 
     Cells(7, st).Interior.Color = vbGreen 

     If st > 1 Then Cells(9, st - 1).Clear 
     Cells(9, st + 1).Clear 
     Cells(9, st).Interior.Color = vbGreen 

     st = st + gr 

     If st > 48 Then gr = -1 

     If st < 2 Then gr = 1 

     Sleep 100 
     DoEvents 
    Wend 
End Sub 
+0

这就是它!!!!!!!! Thaaaaanks :) – Faux

+0

他们可能会从不同的地点开始? – Faux

1

Excel VBA中是单线程再次添加那些3线。

为了有多个宏同时运行,您可以:

  • 启动一个定时器事件(Application.OnTime
  • 每个宏确保每个宏调用的DoEvents定期允许其他并发宏运行。

或者,你可以有你的每一个宏的运行一次(例如画一个红色的单元格),然后在退出之前,请拨打Application.OnTime安排其下执行。

1

如果你想获得几箱的同时来回移动,然后尝试运行RTE()

Declare Sub Sleep Lib "kernel32" (ByVal dwMillisecons As Long) 
Public BegunA As Boolean 
Public BegunB As Boolean 
Public BegunC As Boolean 
Public wf As WorksheetFunction 

Sub RTE() 
    Dim IAmTheCount As Long 
    BegunA = False 
    BegunB = False 
    BegunC = False 
    Set wf = Application.WorksheetFunction 
    IAmTheCount = 1 

    While IAmTheCount < 50 
     Sleep 100 
     DoEvents 
     Call MoveA 
     Call MoveB 
     Call MoveC 
     IAmTheCount = IAmTheCount + 1 
    Wend 

End Sub 
Sub MoveA() 
    Static gr As Long 
    Static st As Long 
    If Not BegunA Then 
     BegunA = True 
     st = wf.RandBetween(2, 9) 
     gr = wf.RandBetween(1, 2) 
     If gr = 2 Then gr = -1 
    End If 

    Cells(5, 1).EntireRow.Clear 
    Cells(5, st).Interior.Color = vbGreen 
    st = st + gr 

    If st > 10 Then 
     gr = -1 
    End If 

    If st < 2 Then 
     gr = 1 
    End If 
End Sub 
Sub MoveB() 
    Static gr As Long 
    Static st As Long 
    If Not BegunB Then 
     BegunB = True 
     st = wf.RandBetween(2, 9) 
     gr = wf.RandBetween(1, 2) 
     If gr = 2 Then gr = -1 
    End If 

    Cells(6, 1).EntireRow.Clear 
    Cells(6, st).Interior.Color = vbYellow 
    st = st + gr 

    If st > 10 Then 
     gr = -1 
    End If 

    If st < 2 Then 
     gr = 1 
    End If 
End Sub 
Sub MoveC() 
    Static gr As Long 
    Static st As Long 
    If Not BegunC Then 
     BegunC = True 
     st = wf.RandBetween(2, 9) 
     gr = wf.RandBetween(1, 2) 
     If gr = 2 Then gr = -1 
    End If 

    Cells(7, 1).EntireRow.Clear 
    Cells(7, st).Interior.Color = vbRed 
    st = st + gr 

    If st > 10 Then 
     gr = -1 
    End If 

    If st < 2 Then 
     gr = 1 
    End If 
End Sub