0
Sub left()  

 Dim m As Integer
 Dim n As Integer
 For m = 0 To 3
  For n = 0 To 3
    If j < 3 Then
      GoTo end
    End If
    If board(m + i - 1, n + j) = 1 Then
      If board(i + m - 1, j + n - 1) = 2 Then
        GoTo end
      End If
    End If
  Next n
 Next m
 j = j - 1
 end:
 End Sub

so im trying to make tetris game on excel and my "moveleft" function doesnt work correctly, when i press left 2 or 3 times very quickly it skips the checking if there is a cell with value of 2(full cell) and goes through full cells. any suggestions?

  • 1
    What is the value of `j`? I recommend you to pass the variable as an argument if you are going to use variables from outside. If is not, put an initial value to it. Also, instead of using `GoTo end` you could use `Exit Sub` and remove the `end` label. – Pablo Díaz Ogni Nov 15 '13 at 21:39
  • j and i are a global variables and they describe "adress" of the first cell of 4x4 figure array – Rimgaudas Tumėnas Nov 15 '13 at 21:45

1 Answers1

3

First, I'd recommend probing the code to see where your jumping out of the command. For example, debug.print "j<3" inside your first if command will tell you if that's where the code is exiting from. Put a different line in multiple spots and watch your immediate window to see what is being processed. You might want to print out your global variables as well to make sure they are what is intended.

Second, don't use end as the goto point since end is a keyword in VBA. I've changed it to endleft

If the speed is an issue then the following two points might help:

Third, since j doesn't change inside the for loops, pull it outside. This is one less eval your doing inside every loop.

Fourth, put your other two if's together into one and use the logical AND. Now your doing one less eval on every loop when the first condition is false.


Sub left()
  Dim m As Integer
  Dim n As Integer
  If Not (j < 3) Then
    For m = 0 To 3
      For n = 0 To 3
        If ((board(m + i - 1, n + j) = 1) And (board(i + m - 1, j + n - 1) = 2)) Then
          GoTo endleft
        End If
      Next n
    Next m
  j = j - 1
  End If
endleft:
End Sub

Fifth, cool project. Will you share it when done?

Automate This
  • 30,726
  • 11
  • 60
  • 82
  • You might be interesed in my Tetris game that I just posted on CodeReview:[Excel VBA Multiplayer Tetris Game Loop Repaint Rate](https://codereview.stackexchange.com/questions/184727/excel-vba-multiplayer-tetris-game-loop-repaint-rate). Here is a download link for my Workbook:[Multiplayer Tetris 2.0](https://drive.google.com/open?id=1ENKZPoI6yq2NRQLCdv0ykEKvHTgJctZC). –  Jan 10 '18 at 07:49