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
 
I'm still searching for the knowledge to create a second combo box with the same values but to search a different range. The error is an ambiguous name detected.
This is how I populated the first combobox. I'm not sure how to make another work without the error occuring or simply having a blank list.

Code:
Sub userform_Initialize()
ComboBox1.List = Array("Agriculture, Forestry, Fishing and Hunting", "Mining, Quarrying, and Oil and Gas Extraction")
End Sub
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The items in that combo box are not coming from a range, they are hard-coded.

to fill a userform combobox from a range, I'd use code like
Code:
Dim oneCell as Range

With ListBox2
    For Each oneCell in ThisWorkbook.Sheet("Sheet1").Range("A1:A10")
        .AddItem CStr(oneCell.Value)
    Next oneCell
End With
 
Upvote 0
I didn't realize sub userform_initialize() would allow you to add more than one array.

Code:
'NAICS Sector Combobox
Sub userform_Initialize()
ComboBox1.List = Array("Agriculture")
ComboBox2.List = Array("Agribusiness")
End Sub

I doubt this is the best way to search, but it is what I have right now. The issue is that if I search for oil it will return and match with oil weather it is oilseed, oil, or boiler.


Code:
'page 2 keyword search
Private Sub CommandButton4_Click()
    Dim Found As Range, FirstFound As String, AllRows As Range
    Dim keysheet As Worksheet
    Set keysheet = ThisWorkbook.Sheets("system")
    
    If TextBox2.Value <> vbNullString Then
        Set Found = keysheet.Cells.Find(TextBox2.Value, LookIn:=xlValues, Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
                         
        If Found Is Nothing Then
            MsgBox "No Match Found.", vbError, "Error"
        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
If you want "oil" to find "oil" or "Oil" but not "olive oil" then set the arguments of the .Find apropriatly.

Lookat:=xlWhole, MatchCase:= False
 
Upvote 0
Unfortunately, there are columns with Mining, Quarrying, and Oil and Gas Extraction as the value. The xlwhole won't find this type of cell.
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,462
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