Error 2042 - Returning row information

cjh28

New Member
Joined
Nov 25, 2009
Messages
15
Morning,

Hope someone can help me out a bit here,

I have workbook with 2 worksheets in it, using vlookup the first spreadsheet pulls information from sheet2 relating to elements in the first worksheet

Currently I have a userform that finds any instance of the user inputted search and tells you which sheet its in and allows u link to them. I also have a number of text boxes that I want populated with the information found on only the activesheet, not both. Currently I get a type mismatch error, and whilst it populates the information in debug mode I also get a Error 2042 on the following code:

TextBox5.Text = Cells(rngFind.Row, 2)

My full code looks like this:

Code:
Option Explicit

Sub locate(Name As String, Data As Range)

    Dim rngFind As Range
    Dim strFirstFind As String
    Dim Find As String
     
    With Data
        Set rngFind = ActiveWorkbook.Sheets(1).Cells.Find(Name, LookIn:=xlValues, LookAt:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstFind = rngFind.Address
                       
            Do
                If rngFind.Row > 1 Then
                    ListBox1.AddItem rngFind.Value
                    ListBox1.List(ListBox1.ListCount - 1, 1) = Data.Parent.Name
                    ListBox1.List(ListBox1.ListCount - 1, 2) = Data.Parent.Name & "!" & rngFind.Address
                     
                 
                    'Find = rngFind.Cells
                    'Location
                   
                    TextBox5.Text = Cells(rngFind.Row, 2)
                   
                    'Speed
                    TextBox10.Text = Cells(rngFind.Row, 6)
                    'TextBox9.Text = Cells(rngFind.Row, 5)
                    'Suitability
                    TextBox7.Text = Cells(rngFind.Row, 4)
                    'IP Range
                    TextBox8.Text = Cells(rngFind.Row, 8)
                                         
                    End If
                Set rngFind = .FindNext(rngFind)
            Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
        End If
    End With
    
End Sub

Private Sub CommandButton1_Click()

    Dim shtSearch As Worksheet
    
       
    ListBox1.Clear
    For Each shtSearch In ThisWorkbook.Worksheets
       
    
        locate TextBox1.Text, shtSearch.Range("A:M")
       
    Next
    If ListBox1.ListCount = 0 Then
        ListBox1.AddItem "No Match Found"
        ListBox1.List(0, 1) = ""
        ListBox1.List(0, 2) = ""
        'ListBox1.List(0, 3) = ""
     
        
    End If
End Sub


Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    
    Dim strSheet As String
    Dim strAddress As String
   
    strSheet = ListBox1.List(ListBox1.ListIndex, 1)
    strAddress = ListBox1.List(ListBox1.ListIndex, 2)
   
    If strAddress <> "" Then
        Worksheets(strSheet).Activate
        Range(strAddress).Activate
        
    End If
End Sub


Private Sub UserForm_Click()

End Sub

Any help would be greatly appreciated

Thanks,
Chris
 
Then you need to do 2 finds, one on Data for the listbox and one on Worksheets("Sitelookup") for the textboxes.
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Would that have to be a seperate sub then to find and populate the textboxes? Any pointers would be great
 
Upvote 0
Ah many thanks, got it working now..

I seem to have botched it together pretty successfully and its all working now.

Here's what I came up with:

Code:
Option Explicit

Sub locate(Name As String, Data As Range)

    Dim rngFind As Range
    Dim strFirstFind As String
    
     
    With Data
        Set rngFind = .find(Name, LookIn:=xlValues, LookAt:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstFind = rngFind.Address
                       
            Do
                If rngFind.Row > 1 Then
                    ListBox1.AddItem rngFind.Value
                    ListBox1.List(ListBox1.ListCount - 1, 1) = Data.Parent.Name
                    ListBox1.List(ListBox1.ListCount - 1, 2) = Data.Parent.Name & "!" & rngFind.Address
                    End If
               Set rngFind = .FindNext(rngFind)
            Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
       End If
    End With
    
End Sub

Sub find(Name As String, Data2 As Range)

    Dim rngFind2 As Range
    Dim strFirstFind As String
    
     
    With Data2
        Set rngFind2 = .find(Name, LookIn:=xlValues, LookAt:=xlPart)
        If Not rngFind2 Is Nothing Then
            strFirstFind = rngFind2.Address

                    'Location
                    With Data2
                    TextBox5.Text = Cells(rngFind2.Row, 2).Text
                    'Speed
                    TextBox10.Text = Cells(rngFind2.Row, 6).Text
                    'Suitability
                    TextBox7.Text = Cells(rngFind2.Row, 4).Text
                    'IP Range5
                    TextBox8.Text = Cells(rngFind2.Row, 8).Text
                    End With
                    End If
               Set rngFind2 = .FindNext(rngFind2)
    End With
End Sub

Private Sub CommandButton1_Click()

    Dim shtSearch As Worksheet
    
       
    ListBox1.Clear
    For Each shtSearch In ThisWorkbook.Worksheets
      
        locate TextBox1.Text, Range("A:M")
        find TextBox1.Text, Worksheets("SiteLookup").Range("A:M")
       
    Next
    If ListBox1.ListCount = 0 Then
        ListBox1.AddItem "No Match Found"
        ListBox1.List(0, 1) = ""
        ListBox1.List(0, 2) = ""
 
     
        
    End If
End Sub

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    
    Dim strSheet As String
    Dim strAddress As String
   
    strSheet = ListBox1.List(ListBox1.ListIndex, 1)
    strAddress = ListBox1.List(ListBox1.ListIndex, 2)
   
    If strAddress <> "" Then
        Worksheets(strSheet).Activate
        Range(strAddress).Activate
        
    End If
End Sub
 
Upvote 0
A few points:

1. I wouldn't use find as the name of a procedure.

2. You are still searching SiteLookup multiple times because its in the loop.

3. You haven't qualified the Cells property so the active sheet will be used and that may not be what you want.
 
Upvote 0
Ok thanks,

Like I said it was a bit of a botch to get it working, I have now renamed the procedures to more descriptive names of the functions.

In terms of searching site lookup multiple times, how can I get away from this?

It will always be the case that the active sheet will be search so Im not too worried about that but I will have a look at that if I get the chance.

Thanks so much for the help ---- much appreciated
 
Upvote 0
Sorry for the delay in gettting back to you, I have now resolved the loop issue. However I have just noticed that If I search for something and its not found in the activesheet i get the error

runtime error '5'

invalid procedure call or argument

Is there any way to avoid this?


Thanks again
 
Upvote 0

Forum statistics

Threads
1,215,883
Messages
6,127,553
Members
449,385
Latest member
KMGLarson

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