Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Possible to do a search/replace in the IDE Code window with VBA?

2003/ 2007

Is it possible, using VBA, to do a search/replace in another VBA module's IDE Code window with VBA?

If so, What objects are involved? Is there a link explaining the process?

TIA EagleOne

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Possible to do a search/replace in the IDE Code window with VBA?

The code below requires a reference to MS VBA Extensibility (whichever version you have). This
does a global replace, so be careful that you are replacing a unique string...

HTH,
Bernie
MS Excel MVP

Sub ReplaceCodeInModule()
Dim myCode As String
Dim WhatToFind As String
Dim ReplaceWith As String
Dim modName As String
Dim myBook As Workbook

WhatToFind = "Hello"
ReplaceWith = "Hello World"
modName = "Module1"

Set myBook = ActiveWorkbook

With myBook.VBProject.VBComponents.Item(modName).CodeMo dule
myCode = .Lines(1, .CountOfLines)
myCode = Replace(myCode, WhatToFind, ReplaceWith)
.DeleteLines 1, .CountOfLines
.InsertLines .CountOfLines + 1, myCode
End With

End Sub

wrote in message
...
2003/ 2007

Is it possible, using VBA, to do a search/replace in another VBA module's IDE Code window with
VBA?

If so, What objects are involved? Is there a link explaining the process?

TIA EagleOne



  #3   Report Post  
Posted to microsoft.public.excel.programming
r r is offline
external usenet poster
 
Posts: 125
Default Possible to do a search/replace in the IDE Code window with VBA?

Check online help about find:
Function Find(Target As String, StartLine As Long, StartColumn As Long,
EndLine As Long, EndColumn As Long, [WholeWord As Boolean = Falso],
[MatchCase As Boolean = Falso], [PatternSearch As Boolean = Falso]) As Boolean
of VBIDE.CodeModule

try ....

Sub test()
Dim WB As Workbook
Set WB = Workbooks("cartel2") '<< change with your name workbook
ReplaceCodeInModule WB, "module1", "Long", "Double", True

End Sub


Sub ReplaceCodeInModule( _
WB As Excel.Workbook, _
sModuleNmae As String, _
sFind As String, _
sReplace As String, _
Optional bWholeWord = False, _
Optional dMatchCase = False, _
Optional bPatternSearch = False)

Dim i As Long
With WB.VBProject.VBComponents.Item(sModuleNmae).CodeMo dule
Do Until .Find(sFind, i, 1, -1, -1, bWholeWord, _
dMatchCase, bPatternSearch) = False
.ReplaceLine i, Replace(.Lines(i, 1), _
sFind, sReplace)
Loop
End With

End Sub

regards
r


Il mio ultimo lavoro ...
http://excelvba.altervista.org/blog/...ternative.html


" wrote:

2003/ 2007

Is it possible, using VBA, to do a search/replace in another VBA module's IDE Code window with VBA?

If so, What objects are involved? Is there a link explaining the process?

TIA EagleOne


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Possible to do a search/replace in the IDE Code window with VBA?

Bernie,

You are an MVP hero! The comprehensiveness of your help is a solution and an applied teaching
moment.

EagleOne

"Bernie Deitrick" <deitbe @ consumer dot org wrote:

The code below requires a reference to MS VBA Extensibility (whichever version you have). This
does a global replace, so be careful that you are replacing a unique string...

HTH,
Bernie
MS Excel MVP

Sub ReplaceCodeInModule()
Dim myCode As String
Dim WhatToFind As String
Dim ReplaceWith As String
Dim modName As String
Dim myBook As Workbook

WhatToFind = "Hello"
ReplaceWith = "Hello World"
modName = "Module1"

Set myBook = ActiveWorkbook

With myBook.VBProject.VBComponents.Item(modName).CodeMo dule
myCode = .Lines(1, .CountOfLines)
myCode = Replace(myCode, WhatToFind, ReplaceWith)
.DeleteLines 1, .CountOfLines
.InsertLines .CountOfLines + 1, myCode
End With

End Sub

wrote in message
.. .
2003/ 2007

Is it possible, using VBA, to do a search/replace in another VBA module's IDE Code window with
VBA?

If so, What objects are involved? Is there a link explaining the process?

TIA EagleOne


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Possible to do a search/replace in the IDE Code window with VBA?

You are an MVP hero! The comprehensiveness of your help is a solution and an applied teaching
moment.


Stop... you're embarrassing me. I'm glad that my post helped you, but it certainly is not
comprehensive, nor is it well documented or explained - I guessed (rightly, it seems) that someone
who wanted to do what you requested could figure it out from the code without a lot of
hand-holding...

Bernie




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Possible to do a search/replace in the IDE Code window with VBA?

Thank you very much to Europe from US

r wrote:

Check online help about find:
Function Find(Target As String, StartLine As Long, StartColumn As Long,
EndLine As Long, EndColumn As Long, [WholeWord As Boolean = Falso],
[MatchCase As Boolean = Falso], [PatternSearch As Boolean = Falso]) As Boolean
of VBIDE.CodeModule

try ....

Sub test()
Dim WB As Workbook
Set WB = Workbooks("cartel2") '<< change with your name workbook
ReplaceCodeInModule WB, "module1", "Long", "Double", True

End Sub


Sub ReplaceCodeInModule( _
WB As Excel.Workbook, _
sModuleNmae As String, _
sFind As String, _
sReplace As String, _
Optional bWholeWord = False, _
Optional dMatchCase = False, _
Optional bPatternSearch = False)

Dim i As Long
With WB.VBProject.VBComponents.Item(sModuleNmae).CodeMo dule
Do Until .Find(sFind, i, 1, -1, -1, bWholeWord, _
dMatchCase, bPatternSearch) = False
.ReplaceLine i, Replace(.Lines(i, 1), _
sFind, sReplace)
Loop
End With

End Sub

regards
r


Il mio ultimo lavoro ...
http://excelvba.altervista.org/blog/...ternative.html


" wrote:

2003/ 2007

Is it possible, using VBA, to do a search/replace in another VBA module's IDE Code window with VBA?

If so, What objects are involved? Is there a link explaining the process?

TIA EagleOne


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Possible to do a search/replace in the IDE Code window with VBA?

Some tease, some pontificate, some provide solutions. Life goes on.

"Bernie Deitrick" <deitbe @ consumer dot org wrote:

You are an MVP hero! The comprehensiveness of your help is a solution and an applied teaching
moment.


Stop... you're embarrassing me. I'm glad that my post helped you, but it certainly is not
comprehensive, nor is it well documented or explained - I guessed (rightly, it seems) that someone
who wanted to do what you requested could figure it out from the code without a lot of
hand-holding...

Bernie

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Possible to do a search/replace in the IDE Code window with VB

Some questions for you, Bernie:

1. In ReplaceCodeInModule, after you delete the original lines N of code,
isn't it illegal to insert at N+1?

2. Are you familiar with the Find method's "patternsearch" parameter, i.e.,
the regular-expression syntax it uses? I saw that someone posted this link
for the syntax, but that page is entitled 'JScript / Regular Expression
Syntax (Scripting)".

http://msdn.microsoft.com/library/de...6f58358c0e.asp

--
Andy Smith
Senior Systems Analyst
Standard & Poor''s, NYC



"Bernie Deitrick" wrote:

The code below requires a reference to MS VBA Extensibility (whichever version you have). This
does a global replace, so be careful that you are replacing a unique string...

HTH,
Bernie
MS Excel MVP

Sub ReplaceCodeInModule()
Dim myCode As String
Dim WhatToFind As String
Dim ReplaceWith As String
Dim modName As String
Dim myBook As Workbook

WhatToFind = "Hello"
ReplaceWith = "Hello World"
modName = "Module1"

Set myBook = ActiveWorkbook

With myBook.VBProject.VBComponents.Item(modName).CodeMo dule
myCode = .Lines(1, .CountOfLines)
myCode = Replace(myCode, WhatToFind, ReplaceWith)
.DeleteLines 1, .CountOfLines
.InsertLines .CountOfLines + 1, myCode
End With

End Sub

wrote in message
...
2003/ 2007

Is it possible, using VBA, to do a search/replace in another VBA module's IDE Code window with
VBA?

If so, What objects are involved? Is there a link explaining the process?

TIA EagleOne




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA line of code executes in Immediate Window but not in Code Window [email protected] Excel Discussion (Misc queries) 2 April 30th 07 02:52 PM
Docking Project Explorer, Properties window and Code window in VBE jayray Setting up and Configuration of Excel 2 March 27th 07 04:42 PM
Duplicated code window and userform window problem Zhu Excel Programming 3 November 8th 06 05:09 AM
Search and replace chunks of html code method373 Excel Discussion (Misc queries) 0 March 27th 06 03:46 PM
Code to search and replace info on modules Brett Smith[_2_] Excel Programming 2 January 12th 06 08:58 PM


All times are GMT +1. The time now is 10:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"