Found Function Help please

Tcarey

New Member
Joined
May 19, 2011
Messages
35
I'm having some trouble making the below code "find" the cells unless I have the location of the data as the active sheet. I need to be on a blank page and bring the rows over onto that page from a seperate sheet.

Private Sub ComboBox1_DropButt*******()
Dim Found As Range, FirstFound As String, AllRows As Range
If ComboBox1.Value <> vbNullString Then

Set Found = ActiveSheet.Cells.Find(ComboBox1.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

If Found Is Nothing Then
MsgBox "No match found.", vbCritical, "No Match"
Else

FirstFound = Found.Address
Set AllRows = Found.EntireRow

Do
Set Found = Cells.FindNext(Found)
Set AllRows = Union(AllRows, Found.EntireRow)

Loop Until Found.Address = FirstFound

AllRows.Copy
Sheets("Risk").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
End If
End If
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try
Code:
Private Sub ComboBox1_DropButt*******()
Dim Found As Range, FirstFound As String, AllRows As Range
[COLOR="Red"]Dim keySheet as Worksheet
Set keySheet = ThisWorkbook.Sheets("searchingSheet")[/COLOR]

If ComboBox1.Value <> vbNullString Then

    Set Found = [COLOR="red"]keySheet[/COLOR].Cells.Find(ComboBox1.Value, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

    If Found Is Nothing Then
        MsgBox "No match found.", vbCritical, "No Match"
    Else

        FirstFound = Found.Address
        Set AllRows = Found.EntireRow

        Do
            Set Found = [COLOR="red"]KeySheeet[/COLOR].Cells.FindNext(Found)
            Set AllRows = Union(AllRows, Found.EntireRow)

        Loop Until Found.Address = FirstFound

        AllRows.Copy
        Sheets("Risk").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
    End If
End If
End Sub
 
Upvote 0
Thanks, that actually clears up a fudemental question I have while learning. I tried adding ---Sheets("system").Activate-- above the set found=... and it worked.

Would you mind helping with one more question? If so, how could I add an error message to the first portion of the code?
 
Upvote 0
Never mind your method is working better. :biggrin:

I added another intro tab and it was pulling that sheet up as the active instead of "system"
 
Upvote 0
When construction is selected from the list I'd like for the find function to return only contruction from B:B. However, it is looking C:E and returning anything with construction in it.
 
Upvote 0
This is a two fold issue for me. First, I cannot figure out how to only "search" in a specific column and return only those matchs. Second, I cannot create another combobox on the same userform with out receiving the ambgious name errorr. Not sure how to get around either of these issues.

Code:
'NAICS Sector Combobox
Sub userform_Initialize()
ComboBox1.List = Array("Agriculture, Forestry, Fishing and Hunting", "Mining, Quarrying, and Oil and Gas Extraction")
End Sub
'NAICS Sector Enter button
Private Sub CommandButton2_Click()
    Dim Found As Range, FirstFound As String, AllRows As Range
    Dim keysheet As Worksheet
    Set keysheet = ThisWorkbook.Sheets("system")
    
    If ComboBox1.Value <> vbNullString Then
        Set Found = keysheet.Cells.Find(ComboBox1.Value, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                         
        If Found Is Nothing Then
            MsgBox "No match found.", vbCritical, "No Match"
        Else
        
            FirstFound = Found.Address
            Set AllRows = Found.EntireRow
            
            Do
                Set Found = keysheet.Cells.FindNext(Found)
                Set AllRows = Union(AllRows, Found.EntireRow)
                
            Loop Until Found.Address = FirstFound
            
            AllRows.Copy
            Sheets("Risk").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
            Range("A2").Select
            
        End If
        End If
    End Sub
 
Upvote 0
The .Find method (and .FindNext) works on the Range to which is is applied.

So to search in column B, you would use
Code:
keySheet.Range("B:B").Find...

The reason that activating the sheet worked is that an unqualified range, like Range("B:B") refers to the ActiveSheet.

It's a good practice to fully qualify ranges specifying Workbook and Worksheet, so that you don't have to keep activating to adjust the unqualified ranges to ActiveSheet.
 
Upvote 0
This addition is not only searching the C:C range. Where am I going wrong?

Code:
'NAICS Sector Enter button
Private Sub CommandButton2_Click()
    Dim Found As Range, FirstFound As String, AllRows As Range
    Dim keysheet As Worksheet
    Set keysheet = ThisWorkbook.Sheets("system")
 
    If ComboBox1.Value <> vbNullString Then
        Set Found = [B]keysheet.Range("C:C").[/B]Find(ComboBox1.Value, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
 
        If Found Is Nothing Then
            MsgBox "No match found.", vbCritical, "No Match"
        Else
 
            FirstFound = Found.Address
            Set AllRows = Found.EntireRow
 
            Do
                Set Found = keysheet.Cells.FindNext(Found)
                Set AllRows = Union(AllRows, Found.EntireRow)
 
            Loop Until Found.Address = FirstFound
 
            AllRows.Copy
            Sheets("Risk").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
            Range("A2").Select
 
        End If
        End If
    End Sub
 
Last edited:
Upvote 0
You also have to apply it to the .FindNext
Code:
Do
    Set Found = keysheet.Range("C:C").Cells.FindNext(Found)
'...
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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