A Find Function

MartinVW

New Member
Joined
Aug 9, 2013
Messages
31
Hey!

I'm currently writing a code but I need to know which range holds a certain. I have the code for the find but Ideally i want to do something like this:

Sub

FindFunction(CertainWord)

Set FoundRange= (I Would like for the code to return the range here so I can set it and continue with my code)

FindFunction(CertainWord)

With Sheets("Output")
Set dFound = .Columns(4).Find(What:=CertainWord, After:=.Cells(1, 4), LookIn:=xlValues, _
Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
End With

End Function

Thank you for the help!
 

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
I'm not sure what you're asking. Do you want to pass a range object between two Subs (procedures)?
 
Upvote 0
I'm sorry. I guess an example is best. Let's say I want to find the text "Systems".

My main sub will first set the value to "Systems"

Then I would like to have a Find Function to search the column on my Output sheet for "Systems" (It will always be there because that is how the sheet is set up)
Once it's found have the function return the range in my main Sub.

Then in my Main sub I can use that range the Find Function has found. And add text underneath the Systems (Such as Windows,MAC)

My problem is I'm not sure about how to get the fund function to return the range into the Main Sub.

Sorry for the confusion.
 
Upvote 0
Code:
[color=darkblue]Sub[/color] Main()
    
    [color=darkblue]Dim[/color] rngFoundWord [color=darkblue]As[/color] Range, strCertainWord  [color=darkblue]As[/color] [color=darkblue]String[/color]
    
    strCertainWord = "System"
    
    [color=darkblue]Set[/color] rngFoundWord = FindFunction(strCertainWord)
    
    [color=darkblue]If[/color] [color=darkblue]Not[/color] rngFoundWord [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
        [color=green]'Match found[/color]
        MsgBox rngFoundWord.Address, , "Here it is"
    [color=darkblue]Else[/color]
        [color=green]'No match found[/color]
        MsgBox "Cannot find '" & strCertainWord & "'. ", , "No Match Found"
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
    
[color=darkblue]Function[/color] FindFunction(strCertainWord [color=darkblue]As[/color] [color=darkblue]String[/color]) [color=darkblue]As[/color] Range
    
    [color=darkblue]With[/color] Sheets("Output")
        [color=darkblue]Set[/color] FindFunction = .Columns(4).Find(What:=strCertainWord, After:=.Cells(1, 4), LookIn:=xlValues, _
            Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Function[/color]
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,360
Members
448,888
Latest member
Arle8907

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