Run-Time error 1004 when clearing cells

irrefutable14

New Member
Joined
Mar 2, 2017
Messages
25
I'm scanning ID & Username into "column A1" the vba then moves the cursor to the right to "column b" finally down to
"A2,B2,etc.... But if I make an error of some sort and click on the cell to delete the ID or Username this Run-time error
'1004': Application-defined or object-defined error pops up? What can I do to get rid of it?

Code:
code:
Private Sub Worksheet_Change(ByVal Target As Range)
    MoveRight = MoveRight + 1
    If MoveRight < 2 Then
        Target.Offset(0, 1).Select
    Else
        Target.Offset(1, -MoveRight + 1).Select
        MoveRight = 0
    End If
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
1004 says that something the code needed to work against doesn't exist. you can create an error routine or ignore errors to keep the code alive, but better to know why it failed and fix that
 
Upvote 0
right. its just a macro that when scanning a barcode it moves in this direction. A1 to B1, down to A2 to B2, down so on and so on. I need to be rapidly scan in items but if its an error delete the error without this code poping up
 
Upvote 0
Your code does not make any sense to me.

The first line is this:
Code:
MoveRight = MoveRight + 1
Since this is the first that "MoveRight" appears in your code, its value will be 1 since 0+1=1.
It will never change, as you are not using any loops.
So, in your IF statement, since MoveRight is 1, it always go to the "Else" clause, and select one row down form where it is now, and set the MoveRight variable to 0, but for what purpose? Every time the code is called, MoveRight is reinitialized.
 
Upvote 0
To be honest I'm a novice to excel. I found the code somewhere on the web and it works halfway.
This is what I need it to do. When I scan a asset code in A1, I need it to go to B2
Then down to the next row A2 over to B2 etc... If there is an error I'd like to delete it without the code shooting an error.
I cant change the direction as though I have 5 sheets to scan on and this sheet is the only one I need to move in that particular direction. Could you help??
 
Last edited:
Upvote 0
See if this does what you want. I documented it to show you what is going on:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Make sure it only runs when data is inserted in a single cell
    If Target.Count > 1 Then Exit Sub

'   Only run when columns A or B are updated with a value
    If Target.Column <= 2 And Target <> "" Then
'       If column A was updated, move to column B in same row
        If Target.Column = 1 Then Target.Offset(0, 1).Select
'       If column B was updated, move to column A in next row
        If Target.Column = 2 Then Target.Offset(1, -1).Select
    End If

End Sub
 
Last edited:
Upvote 0
You are welcome!
Glad I was able to help.:)
 
Upvote 0
If I wanted to add another code to the same sheet is that possible?
It all depends on what you want the code to do. That is the driving force behind these decisions.
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

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