ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Line position in code (https://www.excelbanter.com/excel-programming/311045-re-line-position-code.html)

sebastienm

Line position in code
 
Hi,

- Set a refererence to the MS VBA Extensibnility library:
menu Tools References, add 'Microsoft Visual Basic for Application
Extensilbility Library'
- in the search code, you can use the Find method or you can loop through
the lines, eg:
Dim v As VBProject
Dim c As CodeModule
Dim subName As String
Dim modName As String
Dim startline As Long, endline As Long

modName = "Module1"
subName = "test"

Set v = ThisWorkbook.VBProject
Set c = v.VBComponents(modName).CodeModule

startline = c.ProcStartLine(subName, vbext_pk_Proc)
endline = startline + c.ProcCountLines(subName, vbext_pk_Proc) - 1

'Is the string within the sub code?
MsgBox c.Find("Hello", startline, -1, endline, -1, False, False, False)

'Where is it?
'loop throught the Lines of c: c.Lines(i,1)

Check the ObjectBrowser/OnlineHelp for more details and other methods or
properties of the CodeModule object. Also, 'Find' can search for complex
patterns (Regular Expressions).

Regards,
Sebastien

"Ajit" wrote:

How can i find the number of line for a particular piece of code in a
procedure.
--
Ajit



All times are GMT +1. The time now is 09:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com