Object required error on Worksheet_Change

Jockster

Board Regular
Joined
Jan 16, 2009
Messages
88
The following code is supposed to alert the user if the number they have entered is non-sequential.
When run though, at the Set oldVal line, it errors and I can't figure out what is wrong.
Code:
Sub Worksheet_Change(ByVal Target As Range)
Dim oldVal As Range, newVal As Range
Set Target = Range("A" & Rows.Count).End(xlUp)
With Target
If Target.Value > 1 Then
Set oldVal = Target.Offset(-1, 0).Value
Set newVal = Target.Value
 
 If newVal <> oldVal + 1 Then
 MsgBox "This is not a sequential number", , "Invalid Number?"
 End If
'End If
 End With
 
End Sub
Any help will be appreciated.
Regards.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You've dimmed oldVal and newVal as Ranges, but you're trying to assign them as values...

Change these

Set oldVal = Target.Offset(-1, 0).Value
Set newVal = Target.Value

to

Set oldVal = Target.Offset(-1, 0)
Set newVal = Target
 
Upvote 0
Code:
...
Set oldVal = Target.Offset(-1, 0)
Set newVal = Target
...
 
Upvote 0
Hi there,

You are using Set when you have no object to assign; oldVal and newVal are not Ranges, they are numerical values:

Code:
Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As Long, newval As Long
    Set Target = Range("A" & Rows.Count).End(xlUp)
    With Target
        If Target.Value > 1 Then
            oldVal = Target.Offset(-1, 0).Value
            newval = Target.Value
            If newval <> oldVal + 1 Then
                MsgBox "This is not a sequential number", , "Invalid Number?"
            End If
        End If
    End With
End Sub
 
Upvote 0
Also, I would recommend NOT resetting the Target Object Variable..

It becomes confusing if you're trying to refer to the cell that got changed, or the last occupied cell in column A.
They are not necessarliy (most likely not) the same cell
 
Upvote 0
As per other replies, you've mixed up your variable types and what you are assigning to them. The code works as:
Code:
Sub Worksheet_Change(ByVal Target As Range)
 
Dim oldVal As Long, newVal As Long
 
Set Target = Range("A" & Rows.Count).End(xlUp)
With Target
    If .Value > 1 Then
        oldVal = .Offset(-1, 0).Value
        newVal = .Value
    End If
    If newVal <> oldVal + 1 Then MsgBox "This is not a sequential number", , "Invalid Number?"
End With
 
End Sub
 
Upvote 0
Your macro (well modified code I suggested based on yours), will also fail if you enter any value that is not a positive integer
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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