Cancel button on Input box giving error

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi

This bit of code lets me move rows around in a table using an input box to select the destination.
It works fine except when i click 'cancel' in which case is give a runtime 424 object required error.

any ideas how to fix this?

thanks

Rory

VBA Code:
Sub Move_Selected_Cells()
Dim fromrange As Range
Dim torange As Range

Set fromrange = Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, 22))
Set torange = Application.InputBox(Title:="Select Destination", prompt:="Select where you want to item/s to be moved to then click OK", Type:=8)

ActiveSheet.Unprotect
On Error Resume Next
Application.ScreenUpdating = False

If torange Is Nothing Then GoTo errorhandler
On Error GoTo enableeventson:

Application.EnableEvents = False
Range(torange.Offset(0, -2), torange.Offset(0, 22)).Value = fromrange.Value
fromrange.ClearContents
Application.EnableEvents = True

On Error GoTo 0
Application.ScreenUpdating = True

enableeventson:
Application.EnableEvents = True
ActiveSheet.Protect
errorhandler:
ActiveSheet.Protect
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
hi,
try these changes to your code & see if resolves your issue

Rich (BB code):
Sub Move_Selected_Cells()
Dim fromrange As Range
Dim torange As Range

On Error Resume Next
Set torange = Application.InputBox(Title:="Select Destination", prompt:="Select where you want to item/s to be moved to then click OK", Type:=8)
If torange Is Nothing Then GoTo errorhandler

On Error GoTo enableeventson
Set fromrange = Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, 22))

'rest of code

Dave
 
Upvote 0
Solution
hi,
try these changes to your code & see if resolves your issue

Rich (BB code):
Sub Move_Selected_Cells()
Dim fromrange As Range
Dim torange As Range

On Error Resume Next
Set torange = Application.InputBox(Title:="Select Destination", prompt:="Select where you want to item/s to be moved to then click OK", Type:=8)
If torange Is Nothing Then GoTo errorhandler

On Error GoTo enableeventson
Set fromrange = Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, 22))

'rest of code

Dave
Hi Dave

That worked. Thank you so much :)

Rory
 
Upvote 0
welcome - appreciate your feedback

Dave
 
Upvote 0
SHG answer here may help.

 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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