Find a value and then copying the Row that contains the value

starchildren3317

New Member
Joined
May 17, 2011
Messages
6
I am getting a type mismatch error on a function that I created and can't figure out why. Perhaps one of you can find where I am going wrong.

First some background into what I am trying to do:

I have a workbook with four worksheets. The first one I have created a form to perform the certain tasks. The other three worksheets contain data.

I am trying to look in a worksheet for a value and then copy the entire row that contains that value into the first worksheet.

Here is a snip of what I have:

Code:
Dim FindRange As Range

If chbSM.Value Then [COLOR=SeaGreen]'chbSM is the name of a checbox in my form.[/COLOR]
    FindRange = FindValue("Sample Master") [COLOR=SeaGreen]'FindValue is my function, I am passing in a value to search for. [/COLOR]  
    
    With Worksheets(2).Range(FindRange)
        .Range(.Cells(1), .End(xlToRight)).Copy Destination:=Worksheets("AppSelect").Range("A2")
    End With
    
End If

[COLOR=SeaGreen]'Here is the function[/COLOR] 

Function FindValue(FindWhat As String)

Dim FoundRange As Range

[B]Set FoundRange = Cells.Find(What:=FindWhat, After:=Range("A1").Activate, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)[/B]
        
End Function
When I run this I get a type mismatch error starting on the bolded line of code above.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this on a copy of your data
Code:
Private Function SheetExists(Sheetname As String) As Boolean
    ' Returns TRUE if a sheet exists in the active workbook
    Dim x As Worksheet
        On Error Resume Next
            Set x = ActiveWorkbook.Sheets(Sheetname)
                If Err = 0 Then SheetExists = True _
                Else SheetExists = False
End Function

Sub FindAllSheets()
    Dim Found As Range, WS As Worksheet, LookFor As Variant
        LookFor = InputBox("Enter value to find")
            
            If LookFor = "" Then Exit Sub
            
            '   Clear or Add a Results sheet
            If SheetExists("Search Results") Then
              Sheets("Search Results").Activate
              Range("A2").Select
              Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
              Selection.ClearContents
            Else
                Sheets.Add after:=Sheets(Sheets.Count)
                ActiveSheet.Name = "Search Results"
            End If
            
            For Each WS In ActiveWorkbook.Worksheets
                If WS.Name <> "Search Results" Then
                     Set Found = WS.Cells.Find(What:=LookFor)
                     If Found Is Nothing Then
                         Range("D5").Select
                     Else
                         Found.EntireRow.Copy Sheets("Search results").Cells(Rows.Count, "A").End(xlUp).Offset(1)
                     End If
                End If
            Next WS
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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