 |
| Excel VBA Discuss using VBA for Excel programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 25th, 2007, 11:56 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
What are the WorksheetFunction.Find arguments?
I am given
Quote:
quote: Function Find(Arg1 As String, Arg2 As String, [Arg3]) As Double
Member of Excel.WorksheetFunction
|
in object browser.
When I am looking at WorksheetFunction in Help, and select the link for âMethodsâ then select Find, I am sent to help for a function with a different argument list.
What is Arg1?
What is Arg2?
What is Arg3?
What does this function return if there is no match?
What does this function return when there is a match?
I would like to know those answers even if the following leads to a different approach.
I want to write a routine to search an entire workbook for a string.
My idea is to go to each worksheet in turn and use WorksheesFunction.Find to look for the text I am after.
So naturally, I need to know what the differences in bhavior are for WorsheetFunction.Find when there is or isn't a match found.
|
|

September 25th, 2007, 04:24 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Apparently this function is similar to InStr(). Arg 1 is what to look for, Arg 2 is the string within which to look, and Arg 3 is used to be more specific about where to look.
So how does one get a VBA routine to look for something among all the sheets in the workbook?
|
|

October 12th, 2007, 05:43 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Not sure why you're not using InStr in code.
This is the function that excel uses when you use the following in a cell formula:
=Find(find_text,within_text,[start_num])
Here is an example of it being used in code:
-------------------------------------------------
Private Sub CommandButton1_Click()
Dim sArg1 As String, sArg2 As String, iArg3 As Long, iStartOf As Long
sArg1 = "is"
sArg2 = "This is a string litteral which is cool"
iArg3 = 1
iStartOf = WorksheetFunction.Find(sArg1, sArg2, iArg3)
'StartOf will be position 3 because we're looking at whole string and the first 'is' was found in 'This'.
MsgBox "Value to find:" & vbCrLf & sArg1 _
& vbCrLf & "Content being searched:" & vbCrLf & sArg2 _
& vbCrLf & "Character position to start search from: " & iArg3 _
& vbCrLf & "Found at character position: " & iStartOf
iArg3 = 10
iStartOf = WorksheetFunction.Find(sArg1, sArg2, iArg3)
'StartOf will be position 33 because we're looking starting with (and including) character 10
MsgBox "Value to find:" & vbCrLf & sArg1 _
& vbCrLf & "Content being searched:" & vbCrLf & sArg2 _
& vbCrLf & "Character position to start search from: " & iArg3 _
& vbCrLf & "Found at character position: " & iStartOf
End Sub
-------------------------------------------------
No match returns an error, NOT a 0 value, which puts #value! as a cell value when used in a formula.
Even if you're sure you'll always have a positive hit I'd still suggest trapping for it.
Hope this helped.
|
|

October 24th, 2007, 12:21 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Looks like I didn't answer the second part of the question. Sorry.
The following code sample goes through each Worksheet in the current workbook and marks cells that contain a given value. I assigned this code to a button I put on the first worksheet:
----------------------------------------------------
Private Sub CommandButton1_Click()
Dim oSheetOn As Worksheet, sToFind As String, iRowOn As Long
sToFind = "de"
For Each oSheetOn In ActiveWorkbook.Worksheets
iRowOn = 1
Do While oSheetOn.Cells(iRowOn, 1).Value <> ""
If InStr(1, oSheetOn.Cells(iRowOn, 1).Value, sToFind) > 0 _
Then oSheetOn.Cells(iRowOn, 1).Interior.Color = RGB(200, 200, 200)
DoEvents
iRowOn = iRowOn + 1
Loop
Next
End Sub
-----------------------------------------------------
If you still want to use the worksheetfunction.find then try this code:
-----------------------------------------------------
Private Sub CommandButton1_Click()
Dim oSheetOn As Worksheet, sToFind As String, iRowOn As Long
sToFind = "de"
For Each oSheetOn In ActiveWorkbook.Worksheets
iRowOn = 1
Do While oSheetOn.Cells(iRowOn, 1).Value <> ""
If FindIt(oSheetOn.Cells(iRowOn, 1), sToFind) > 0 _
Then oSheetOn.Cells(iRowOn, 1).Interior.Color = RGB(200, 200, 200)
DoEvents
iRowOn = iRowOn + 1
Loop
Next
End Sub
Private Function FindIt(oCell As Range, sToFind As String) As Long
On Error GoTo FailedFindIt
FindIt = WorksheetFunction.Find(sToFind, oCell.Value, 1)
On Error GoTo 0
FailedFindIt:
End Function
------------------------------------------------
|
|
 |