Excel VBA同时启动多个宏

时间:2015-05-24 11:54:01

标签: excel vba excel-vba

我有这个代码将(B;5)单元格描绘成红色,然后开始来回移动它。

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)单元格并同时开始移动它们?

3 个答案:

答案 0 :(得分:2)

您的代码

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

处理第5行。只需再添加3行,即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

答案 1 :(得分:1)

Excel VBA是单线程的。

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

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

或者,您可以让每个宏运行一次(例如将单元格绘制为红色),然后在退出之前,调用Application.OnTime以安排其下一次执行。

答案 2 :(得分: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