View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default get linenumber when string found in VBE codemodule

Tom,

Thanks for that.
This is a new one for me. Very useful to know!

The msgbox in your example should be:
MsgBox i & " " & j & " " & k


RBS


"Tom Ogilvy" wrote in message
...
Unlike most excel/vba functions, the arguments that Myrna shows are also
for
output. Not only does the function return true or false, it also sets
values to the arguments which you can read with you variable

a sample home made function to illustrate

Sub Main
Dim i, j, k

i = 1
j = 2
k = 3
i = Myfunction(j, k)
msgbox i, j, k

End Sub

Function MyFunction( s, t)
s = s * 2
t = t * 2
Myfunction = s * t
End Function

Msbox show 24 4 6

so the function returns 3 values. One the normal way and 2 through the
argument list.


so your function would be

Function GetLineNumberString(ByRef vbProj As VBProject, _
ByRef VBComp As VBComponent, _
ByVal strProc As String, _
ByVal strToFind As String) As Long

Dim i As Long
Dim lStartRow As Long
Dim lCount As Long
Dim lEnd As Long
Dim lStartCol as Long
Dim vbMod As CodeModule

Set vbMod = VBComp.CodeModule

With vbMod
lStartRow = .ProcBodyLine(strProc, vbext_pk_Proc)
lCount = .ProcCountLines(strProc, vbext_pk_Proc)
lEnd = lStart + lCount
If .Find(strToFind, lStartRow, lStartCol , i, -1, False, False, False) =
True
GetLineNumberString = lStartRow
Else
GetLineNumberString = 0
End if
End With
End Function


--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
Myrna,

As far as I can see the Find method of the VBE library only returns TRUE

or
FALSE.

I have just made a function that loops through the lines. It works, but I
would be interested
if there was a direct way to get the line number where string was found
first.


Function GetLineNumberString(ByRef vbProj As VBProject, _
ByRef VBComp As VBComponent, _
ByVal strProc As String, _
ByVal strToFind As String) As Long

Dim i As Long
Dim lStart As Long
Dim lCount As Long
Dim lEnd As Long
Dim vbMod As CodeModule

Set vbMod = VBComp.CodeModule

With vbMod
lStart = .ProcBodyLine(strProc, vbext_pk_Proc)
lCount = .ProcCountLines(strProc, vbext_pk_Proc)
lEnd = lStart + lCount
For i = lStart To lEnd
If .Find(strToFind, i, 1, i, -1, False, False, False) = True
Then
GetLineNumberString = i
Exit Function
End If
Next
End With

GetLineNumberString = 0

End Function

Sub linetest()

MsgBox GetLineNumberString(ThisWorkbook.VBProject, _

ThisWorkbook.VBProject.VBComponents("SubsFromText" ),
_
"UnlinkedDrugsStatsPivot", _
"pivot table")
End Sub


RBS


"Myrna Larson" wrote in message
...
How are you searching for the string? If it's via the Find method in
the
VBIDE
library, you find the following in Help.

Note the comments re the StartLine argument, specifically that if the
string
is found, the argument is set to the line number where it was found.

Is that what you need?

"Find Method (VBA Add-In Object Model)

Searches the active module for a specified string.

Syntax

object.Find(target, startline, startcol, endline, endcol [, wholeword]

[,
matchcase] [, patternsearch]) As Boolean

object Required. An object expression that evaluates to an object in
the
Applies To list.

target Required. A String containing the text or pattern you want to

find.

startline Required. A Long specifying the line at which you want to

start
the
search; WILL BE SET TO THE LINE OF THE MATCH IF ONE IS FOUND. The first
line
is number 1.

startcol Required. A Long specifying the column at which you want to

start
the
search; will be set to the column containing the match if one is found.
The
first column is 1.

endline Required. A Long specifying the last line of the match if one
is
found. The last line may be specified as -1.

endcol Required. A Long specifying the last line of the match if one is
found.
The last column may be designated as -1.

wholeword Optional. A Boolean value specifying whether to only match

whole
words. If True, only matches whole words. False is the default.

matchcase Optional. A Boolean value specifying whether to match case.
If
True,
the search is case sensitive. False is the default.

patternsearch Optional. A Boolean value specifying whether or not the
target
string is a regular expression pattern. If True, the target string is a
regular expression pattern. False is the default.


On Sat, 9 Oct 2004 19:22:20 +0100, "RB Smissaert"
wrote:

How do I programmatically get the linenumber of the first line in a
code
module where a specified string is found?
I can see if a string was found or not, but there seems to be no way
to
get
the line number, other
than running a loop and checking a number of lines.

RBS