Application-defined or object-defined error (VBA)

bencameron137

New Member
Joined
Jun 6, 2018
Messages
3
https://drive.google.com/file/d/1fsl-nLcgM2Z8p4anOg_JW8fBndei59qG/view?usp=sharing

I have a worksheet that uses VBA to replicate an old game called 2048 (https://gabrielecirulli.github.io/2048/)

The game works alright, there are a few issues I need to iron out, but there's one main problem I need help with.

I added a function (WinLoseCheck) to both check if you had won (achieving a 2048 square), in which it would tell you, and to check if you lost (not being able to move), in which it would also tell you.

The winning system worked, it was just used a for each to determine if any of the cells were 2048. The losing system, however, had to check if there was a space to move into, and if any of the cells could merge. When I implemented this, it threw a application-defined or object-defined error.

If anyone could tell me why this is happening and a solution, I would be ever grateful. Thanks in advance.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to the Forum!

Based on a very quick look at your code (not sure if this is where you are getting the error, but it will generate a run time error 1004) ...

Code:
Set RNG = Range("A1:D4")
For Each r In RNG
    If r.Value <> "" And r.Value <> r.Offset(0, -1) And r.Value <> r.Offset(0, 1) And r.Value <> r.Offset(-1, 0) And r.Value <> r.Offset(1, 0) Then
    '...

When r is cell A1, you can't offset to the previous row, or to the previous column.

When you check previous row, you'll need to test rows 2-4 and not 1, and similarly when you check previous column, you'll need to test columns 2-4 and not 1.
 
Upvote 0
Welcome to the Forum!

Based on a very quick look at your code (not sure if this is where you are getting the error, but it will generate a run time error 1004) ...

Code:
Set RNG = Range("A1:D4")
For Each r In RNG
    If r.Value <> "" And r.Value <> r.Offset(0, -1) And r.Value <> r.Offset(0, 1) And r.Value <> r.Offset(-1, 0) And r.Value <> r.Offset(1, 0) Then
    '...

When r is cell A1, you can't offset to the previous row, or to the previous column.

When you check previous row, you'll need to test rows 2-4 and not 1, and similarly when you check previous column, you'll need to test columns 2-4 and not 1.

How would I achieve this?
 
Upvote 0
How would I achieve this?

Here's how I'd do it:

Code:
Dim rng As Range
Dim vNos As Variant
Dim bCanGo As Boolean
Dim i As Long, j As Long

Set rng = Range("A1:D4")
If Application.CountIf(rng, ">0") = rng.Cells.Count Then
    vNos = rng.Value2
    For i = 1 To UBound(vNos) - 1
        For j = 1 To UBound(vNos, 2) - 1
            If vNos(i, j) = vNos(i + 1, j) Or vNos(i, j) = vNos(i, j + 1) Then
                bCanGo = True
                GoTo EndLoop
            End If
        Next j
    Next i
    If vNos(i, j) = vNos(i - 1, j) Or vNos(i, j) = vNos(i, j - 1) Then bCanGo = True
Else
    bCanGo = True
End If

EndLoop:
If bCanGo Then
    'User still has moves available
Else
    'User is stuck
End If
 
Upvote 0
Here's how I'd do it:

Code:
Dim rng As Range
Dim vNos As Variant
Dim bCanGo As Boolean
Dim i As Long, j As Long

Set rng = Range("A1:D4")
If Application.CountIf(rng, ">0") = rng.Cells.Count Then
    vNos = rng.Value2
    For i = 1 To UBound(vNos) - 1
        For j = 1 To UBound(vNos, 2) - 1
            If vNos(i, j) = vNos(i + 1, j) Or vNos(i, j) = vNos(i, j + 1) Then
                bCanGo = True
                GoTo EndLoop
            End If
        Next j
    Next i
    If vNos(i, j) = vNos(i - 1, j) Or vNos(i, j) = vNos(i, j - 1) Then bCanGo = True
Else
    bCanGo = True
End If

EndLoop:
If bCanGo Then
    'User still has moves available
Else
    'User is stuck
End If

Works perfectly! Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,822
Members
449,469
Latest member
Kingwi11y

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top