Run-time error '1004 Method range of object worksheet failed

Dan Swartz

Board Regular
Joined
Apr 17, 2020
Messages
51
Office Version
  1. 365
Platform
  1. Windows
So I have the following code and it does exactly what I need. However, if I move, copy or delete contents in more than one cell at a time, I get a run time error, and no matter if I select "End" or "Debug", It crashes Excel. I'm assuming I'm missing some error handling code, but I'm an amateur with code. I have searched and searched and I find no answer to my problem.

Private Sub Worksheet_Change(ByVal Target As Range)

WoodSpecies = Target.Value
If Target.Column = 5 Then
WoodSpeciesNumber = Application.VLookup(WoodSpecies, Sheet20.Range("WoodSpeciesClazakNumber"), 2, False)
If Not IsError(WoodSpeciesNumber) Then
Target.Value = WoodSpeciesNumber
End If
End If
End Sub

Basically, this code allows me to select a wood species from a dropdown list, then will change it to a part number after it's entered.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
One of the problems is that you have what is effectively a permanent looping running. Try
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 5 Then
      WoodSpecies = Target.Value
      WoodSpeciesNumber = Application.VLookup(WoodSpecies, Sheet20.Range("WoodSpeciesClazakNumber"), 2, False)
      If Not IsError(WoodSpeciesNumber) Then
         On Error GoTo Xit
         Application.EnableEvents = False
         Target.Value = WoodSpeciesNumber
      End If
   End If
Xit:
   Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
Add at the beginning of this event procedure this codeline : If Target.CountLarge > 1 Then Exit Sub …​
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,815
Messages
6,121,715
Members
449,049
Latest member
THMarana

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