Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Replacing code

I have about 200 files where I need to change one word in the code
from "prop" to "east".
Instead of opening each file and doing it manually, it would be great
if I could write a macro to rewrite the code for me.

Here's what I have so far which Finds the word in the code. But I do
not know how to go about replacing it.

Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim FindWhat As String
Dim ReplaceWhat As String
Dim SL As Long ' start line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim Found As Boolean

Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Thisworkbook")
Set CodeMod = VBComp.CodeModule

FindWhat = "prop"
ReplaceWhat = "east"

With CodeMod
SL = 1
EL = .CountOfLines
SC = 1
EC = 255
Found = .Replace(target:=FindWhat, replacement:="east",
StartLine:=SL, StartColumn:=SC, _
EndLine:=EL, EndColumn:=EC, _
wholeword:=True, MatchCase:=False,
patternsearch:=False)

End With


Any help is appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Replacing code

Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim FindWhat As String
Dim ReplaceWhat As String
Dim OldLine As String
Dim NewLine As String
Dim SL As Long ' start line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim Found As Boolean

Workbooks.Open ("C:\test\Test Test Test.xls")
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Thisworkbook")
Set CodeMod = VBComp.codemodule

FindWhat = "prop"
ReplaceWhat = "east"

SL = 1
With CodeMod

Do
Found = .Find(FindWhat, SL, SC, EL, EC, True, False, False)
If Found Then

OldLine = .Lines(SL, 1)
NewLine = Left$(OldLine, SC - 1) & ReplaceWhat
If EC < Len(OldLine) Then NewLine = NewLine &
Right$(OldLine, Len(OldLine) - EC + 1)
.ReplaceLine SL, NewLine
SL = SL + 1
End If
Loop Until Not Found
End With


--
__________________________________
HTH

Bob

wrote in message
...
I have about 200 files where I need to change one word in the code
from "prop" to "east".
Instead of opening each file and doing it manually, it would be great
if I could write a macro to rewrite the code for me.

Here's what I have so far which Finds the word in the code. But I do
not know how to go about replacing it.

Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim FindWhat As String
Dim ReplaceWhat As String
Dim SL As Long ' start line
Dim EL As Long ' end line
Dim SC As Long ' start column
Dim EC As Long ' end column
Dim Found As Boolean

Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("Thisworkbook")
Set CodeMod = VBComp.CodeModule

FindWhat = "prop"
ReplaceWhat = "east"

With CodeMod
SL = 1
EL = .CountOfLines
SC = 1
EC = 255
Found = .Replace(target:=FindWhat, replacement:="east",
StartLine:=SL, StartColumn:=SC, _
EndLine:=EL, EndColumn:=EC, _
wholeword:=True, MatchCase:=False,
patternsearch:=False)

End With


Any help is appreciated.



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
Replacing Code with words using vlookup Arnold Klapheck Excel Programming 3 July 19th 07 12:46 AM
question about excessive code in replacing #N/A when using VLOOKUP njuneardave Excel Discussion (Misc queries) 4 June 21st 06 03:14 AM
Replacing VBA code strings by using VBA code? shoba[_2_] Excel Programming 1 June 27th 05 10:42 PM
Replacing code at runtime Bernie Deitrick Excel Programming 6 September 17th 04 05:55 PM
Replacing code at runtime Ajit Excel Programming 0 September 17th 04 05:15 PM


All times are GMT +1. The time now is 10:12 AM.

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"