How to replace a line of code with a multi-line code and vice-versa

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
358
Office Version
  1. 2003 or older
Platform
  1. Windows
The following subroutine replaces a single line of code with another line across the module. How can this be modified to either replace a single line with a multi-line code ir vuce versa?

VBA Code:
Sub ReplaceCodeModuleText(strModule As String, strFindWhat As String, strReplaceWith As String)

    Dim VBProj As VBProject
    Dim VBComp As VBComponent
    Dim CodeMod As CodeModule

    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 strCodeLine As String
    Dim vDummy As Variant

    Dim Found As Boolean

    Set VBProj = Application.VBE.ActiveVBProject
    Set VBComp = VBProj.VBComponents(strModule)
    Set CodeMod = VBComp.CodeModule

    With CodeMod
        SL = 1: EL = .CountOfLines
        SC = 1: EC = 255

        Found = .Find(Target:=strFindWhat, StartLine:=SL, StartColumn:=SC, _
                      EndLine:=EL, EndColumn:=EC, _
                      wholeword:=True, MatchCase:=False, PatternSearch:=False)

        If Found Then
            strCodeLine = CodeMod.Lines(SL, 1)
            strCodeLine = Replace(strCodeLine, strFindWhat, strReplaceWith, Compare:=vbTextCompare)    ' Not case sensitive = vbTextCompare
            .ReplaceLine SL, strCodeLine

            Debug.Print "Successfully Replaced: " & strFindWhat & " in VBA Module: " & strModule & " with : " & strReplaceWith
        Else
            Debug.Print "Did not find: " & strFindWhat;

        End If
    End With
End Sub

Sub Demo()
    Call ReplaceCodeModuleText("Module1", "Application.Calculation = xlCalculationManual", "Application.Calculation = xlCalculationManual" & Chr(13) & Chr(10) & "Application.EnableEvents = False")
End Sub

Say for example, replace all occurrences of:
Application.Calculation = xlCalculationManual (single line)
with
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual (two lines of code)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You could use DeleteLines and InsertLines instead.
 
Upvote 0
@RoryA ; Thanks ... will InsertLines need a position to be specified? I want to replace independent of position.
 
Upvote 0
Yes, just like ReplaceLine, it needs a position - how else would it know where to put the lines?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top