Run-time error '1004': Method 'Range' of object '_worksheet' failed

Shaunch

New Member
Joined
May 17, 2021
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Hi
I have looked at the threads that have this heading but cannot see one that matches my situation and after looking at them all I am now confused
any help would be great

my code is

Private Sub cboName_Change()

Dim Found As Range
Dim str As String

str = Me.cboName.Value
Set Found = Sheet2.Range("D9", Range("D" & Rows.Count).End(xlUp)).Find(str) this is where i get the error when it searches the name selected

If Found Is Nothing Then
MsgBox ("Not Found!")
Else
Me.txtStart = Found.Row
Me.txtHours = Cells(Found.Row, 3).Value
Me.txtnumber = Cells(Found.Row, 5).Value
Me.txtDepartment = Cells(Found.Row, 6).Value
Me.txtSex = Cells(Found.Row, 7).Value
Me.txtFIREWARDEN = Cells(Found.Row, 9).Value
Me.txtFIRSTAID = Cells(Found.Row, 10).Value
Me.txtHr = Cells(Found.Row, 11).Value
Me.txtCold = Cells(Found.Row, 13).Value
End If

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,

It seems to work, I assigned "str" a string and it found the string in Column D, see the screenshot. When or where does the "Me.cboName.Value" pull from? You may need to make "str" a global variable and assign it when "Me.cboName.Value" pulls its value.
 

Attachments

  • forRunTimeError1004.jpg
    forRunTimeError1004.jpg
    99.2 KB · Views: 10
Upvote 0
Even if you change to variant, the problem below still exist:

When or where does the "Me.cboName.Value" pull from?
 
Upvote 0
When or where you assign "Me.cboName.Value", assign "str" to it with:

Global Dim str As String
str = Me.cboName.Value

and comment out the current Dim str as string
then you can use str in that private subroutine.
 
Upvote 0
You need to qualify the second Range call with the same sheet:

Rich (BB code):
Set Found = Sheet2.Range("D9", Sheet2.Range("D" & Rows.Count).End(xlUp)).Find(str)
 
Upvote 0
Since @RoryA's solution also flushes out that Sheet2 is not the active sheet, you are going to need to qualify all your uses of "Cells"
ie
VBA Code:
Private Sub cboName_Change()

    Dim Found As Range
    Dim str As String
   
    str = Me.cboName.Value
   
    With Sheet2
        Set Found = .Range("D9", .Range("D" & Rows.Count).End(xlUp)).Find(str)
       
        If Found Is Nothing Then
        M sgBox("Not Found!")
        Else
            Me.txtStart = Found.Row
            Me.txtHours = .Cells(Found.Row, 3).Value
            Me.txtnumber = .Cells(Found.Row, 5).Value
            Me.txtDepartment = .Cells(Found.Row, 6).Value
            Me.txtSex = .Cells(Found.Row, 7).Value
            Me.txtFIREWARDEN = .Cells(Found.Row, 9).Value
            Me.txtFIRSTAID = .Cells(Found.Row, 10).Value
            Me.txtHr = .Cells(Found.Row, 11).Value
            Me.txtCold = .Cells(Found.Row, 13).Value
        End If
    End With

End Sub
 
Upvote 0
And do not forget to also qualify the Rows.Count as well.

It is possible to get an "Application - defined or Object-defined" error if an Excel 2003 worksheet is active when you are running this code on a Excel 2007+ File. Here is a CLASSIC EXAMPLE where this error may occur.

I always advise using "." before Rows.Count and Columns.Count. As mentioned your code may fail because the Rows.Count returns 65536 for Excel 2003 and earlier and 1048576 for Excel 2007 and later. Similarly Columns.Count returns 256 and 16384, respectively.

It is highly advisable that you fully qualify ALL your objects. LEAVE NO MAN BEHIND, er I MEAN CODE UNQUALIFIED! :)

VBA Code:
With Sheet2
    Set Found = .Range("D9", .Range("D" & .Rows.Count).End(xlUp)).Find(Str)
End With
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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