VBA - Run-time error 1004 - Application-defined or object-defined error

L

Legacy 502047

Guest
Hi everyone,

I'm working on making a version of 2048 in excel and trying to only use 2 variables to control all 4 directions of movement.
It works perfectly moving the tiles up or to the left, when direction2 = 1; but when direction2 = -1 to move them the opposite direction it throws up the error mentioned in the title of this thread.

I've been trying fix it for a few days now with no luck, wondering if someone could point me in the right direction?

Function MoveTiles(direction1 As Integer, direction2 As Integer, row As Integer, col As Integer)
Dim i, j, amount As Integer

If direction1 = 1 And direction2 = 1 Then 'up
i = 1
amount = boardcolumns
ElseIf direction1 = 1 And direction2 = -1 Then 'down
i = boardcolumns
amount = 1
ElseIf direction1 = -1 And direction2 = 1 Then 'left
i = 1
amount = boardrows
ElseIf direction1 = -1 And direction2 = -1 Then 'right
i = boardrows
amount = 1
End If

Do While (i <> amount)
If direction1 = 1 Then 'vertical/columns
If Cells(i, col).Value <> "" And Cells(i, col).Value = Cells(i + direction2, col).Value Then
Cells(i, col).Value = Cells(i, col).Value + Cells(i + direction2, col).Value
Cells(i + direction2, col).Value = ""
For j = i + direction2 To amount Step direction2
Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working
Next j
ElseIf Cells(i, col).Value = "" And TilesToBeMoved(direction1, direction2, (i), col) = True Then
For j = i To amount Step direction2
Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working
Next j
Else
i = i + direction2
End If
ElseIf direction1 = -1 Then 'horizontal/rows
If Cells(row, i).Value <> "" And Cells(row, i).Value = Cells(row, i + direction2).Value Then
Cells(row, i).Value = Cells(row, i).Value + Cells(row, i + direction2).Value
Cells(row, i + direction2).Value = ""
For j = i + direction2 To amount Step direction2
Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working
Next j
ElseIf Cells(row, i).Value = "" And TilesToBeMoved(direction1, direction2, row, (i)) = True Then
For j = i To amount Step direction2
Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working
Next j
Else
i = i + direction2
End If
End If
Loop
End Function

Many thanks,
yuro0804
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I can make 2 suggestions:
1) Step through your code (F8) and watch your variables and see if they are what you expect. You should be able to know if you mouse over them, but you have to execute the line first. That means when this line is highlighted
For j = i + direction2 To amount Step direction2

i won't have a value unless a previous line assigned it to that variable, so you have to move down one more line before checking.

2) post code using vba code tags (vba icon on posting toolbar) to maintain indentation and readability.
Tips:
- always make it clear which line raises the error. It may be where you posted 'move right not working - but maybe not.
- Dim i, j, amount As Integer: j is an Integer, i is a variant because it is not explicitly declared. Sometimes that can be the cause of errors (the variable is a variant, and not what you think it is).
 
Upvote 0
I can make 2 suggestions:
1) Step through your code (F8) and watch your variables and see if they are what you expect. You should be able to know if you mouse over them, but you have to execute the line first. That means when this line is highlighted
For j = i + direction2 To amount Step direction2

i won't have a value unless a previous line assigned it to that variable, so you have to move down one more line before checking.

2) post code using vba code tags (vba icon on posting toolbar) to maintain indentation and readability.
Tips:
- always make it clear which line raises the error. It may be where you posted 'move right not working - but maybe not.
- Dim i, j, amount As Integer: j is an Integer, i is a variant because it is not explicitly declared. Sometimes that can be the cause of errors (the variable is a variant, and not what you think it is).
Hi, thanks for your reply. I realised I never underlined the lines with the errors until after I posted it, and couldn't find a way to edit the post, it is the lines with the comments as you said however. I didn't know you could format the code to keep the indentations so I'll post it again making the changes you suggested.

I assumed Dim i, j, amount As Integer would set them all to be integers as it doesn't say otherwise, I didn't know it would just default them as variant instead, good to know.
I stepped through the code and the variables seem to be good, so there's something else causing it as I set everything to integer as I originally intended.

VBA Code:
Function MoveTiles(direction1 As Integer, direction2 As Integer, row As Integer, col As Integer)
Dim i As Integer, j As Integer, amount As Integer

If direction1 = 1 And direction2 = 1 Then 'up
    i = 1
    amount = boardcolumns
ElseIf direction1 = 1 And direction2 = -1 Then 'down
    i = boardcolumns
    amount = 1
ElseIf direction1 = -1 And direction2 = 1 Then 'left
    i = 1
    amount = boardrows
ElseIf direction1 = -1 And direction2 = -1 Then 'right
    i = boardrows
    amount = 1
End If

Do While (i <> amount)
    If direction1 = 1 Then 'vertical/columns
        If Cells(i, col).Value <> "" And Cells(i, col).Value = Cells(i + direction2, col).Value Then
            Cells(i, col).Value = Cells(i, col).Value + Cells(i + direction2, col).Value
            Cells(i + direction2, col).Value = ""
            For j = i + direction2 To amount Step direction2
                [U]Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working (when direction2 = -1)[/U]
            Next j
        ElseIf Cells(i, col).Value = "" And TilesToBeMoved(direction1, direction2, (i), col) = True Then
            For j = i To amount Step direction2
                [U]Cells(j, col).Value = Cells(j + direction2, col).Value 'move down not working (when direction2 = -1)[/U]
            Next j
        Else
            i = i + direction2
        End If
    ElseIf direction1 = -1 Then 'horizontal/rows
        If Cells(row, i).Value <> "" And Cells(row, i).Value = Cells(row, i + direction2).Value Then
            Cells(row, i).Value = Cells(row, i).Value + Cells(row, i + direction2).Value
            Cells(row, i + direction2).Value = ""
            For j = i + direction2 To amount Step direction2
                [U]Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working (when direction2 = -1)[/U]
            Next j
        ElseIf Cells(row, i).Value = "" And TilesToBeMoved(direction1, direction2, row, (i)) = True Then
            For j = i To amount Step direction2
                [U]Cells(row, j).Value = Cells(row, j + direction2).Value 'move right not working (when direction2 =-1)[/U]
            Next j
        Else
            i = i + direction2
        End If
    End If
Loop
End Function
 
Upvote 0
Sorry, I can't see what's wrong just by looking at that. You might have noticed by now that trying to format code in tags doesn't work. Best to be clear, like
VBA Code:
Cells(j, col).Value = Cells(j + direction2, col).Value 'raises error here
Make sure you provide error number as well as message as there are 1000's of them. Perhaps try substituting actual values rather than variables?
 
Upvote 0
Sorry, I can't see what's wrong just by looking at that. You might have noticed by now that trying to format code in tags doesn't work. Best to be clear, like
VBA Code:
Cells(j, col).Value = Cells(j + direction2, col).Value 'raises error here
Make sure you provide error number as well as message as there are 1000's of them. Perhaps try substituting actual values rather than variables?
Yeah I noticed it haha, so annoying you can't edit posts.

Error message and number and in the thread title, but 'run-time error 1004: application-defined or object-defined error' occurs on all the commented lines.

I tried substituting the values before in a previous attempt/version with no success, but I'll try with this one good idea. Thanks for you help.

I can post the other functions as well, but it seems to get through them fine and they work for the up and left directions so they seem fine to me.
 
Upvote 0
Yeah, I was clear on the message but "not working" is something I see all the time and can be interpreted as just that. 1004 has to be about the most useless number given that it must be used for 10 or more problems.
FYI - you can edit, but only for 10 minutes.
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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