programmaticly change code

  • Thread starter Thread starter bruce
  • Start date Start date
B

bruce

I want to have a hard coded value for a variable and then
be able to use code to alter the hardcoded value.
I have seen advice regarding how to get basic info like
the number of lines of code in a routine etc but nothing
that shows how to find & replace a line of code with new
code.
 
Bruce,

Try something like the following. It will change the first occurrence in
Module2 of

X = 123
to
X =234


Dim SL As Long
Dim EL As Long
Dim SC As Long
Dim EC As Long
Dim Found As Boolean

With ThisWorkbook.VBProject.VBComponents("Module2").CodeModule
SL = 1
EL = .CountOfLines
SC = 1
EC = 1024
Found = .Find("X = 123", SL, SC, EL, EC, True, False, False)
If Found = True Then
.DeleteLines SL
.InsertLines SL, "X = 234"
End If
End With
 
Back
Top