Consulting

Results 1 to 4 of 4

Thread: VBA to modify another VBA

  1. #1

    Question VBA to modify another VBA

    Hi,

    I am trying to create a VBA that will:
    -find and replace a specific string
    -in all other VBAs of the same workbook
    -any number of time(s), in any part(s) and any VBA(s) it appears

    I have found this example, but it replaces the whole line, instead of the desired string only:

    Sub replaceConstant()
        Dim project As VBIDE.VBProject
        For Each project In Application.VBE.VBProjects
     
            Dim codeMod As VBIDE.CodeModule
            Dim component As VBIDE.VBComponent
     
            For Each component In project.VBComponents
     
                If component.Name <> "replaceConstant" Then
     
                    Set codeMod = component.CodeModule
     
                    Dim startline As Long
                    startline = 1
     
                    codeMod.Find Target:="Old_Text", _
                        startline:=startline, startcolumn:=1, endline:=codeMod.CountOfLines, endcolumn:=1
     
                    codeMod.ReplaceLine startline, "New_text"
     
     
                End If
            Next component
     
        Next project
    End Sub
    Could you please help me?
    Last edited by Bob Phillips; 05-03-2022 at 08:43 AM. Reason: Added code tags

  2. #2
    Sub replaceConstant()Dim project As VBIDE.VBProject
    Dim codeMod As VBIDE.CodeModule
    Dim component As VBIDE.VBComponent
    Dim n As Long, s As String
    
    
    For Each project In Application.VBE.VBProjects
    
    
        For Each component In project.VBComponents
        
            If component.Name <> "replaceConstant" Then
            
                Set codeMod = component.CodeModule
                
                With codeMod
                    For n = 1 To .CountOfLines
                        s = .Lines(n, 1)
                        If Len(Trim$(s)) Then
                            If s Like "*" & Old_Text & "*" Then
                                s = Replace$(s, Old_Text, New_Text)
                                .DeleteLines n, 1
                                .InsertLines n, s
                            End If
                        End If
                    Next
                End With
                'Dim startline As Long
                'startline = 1
                
                'codeMod.Find Target:="Old_Text", _
                'startline:=startline, startcolumn:=1, endline:=codeMod.CountOfLines, endcolumn:=1
                
                'codeMod.ReplaceLine startline, "New_text"
            
            
            End If
        Next component
    
    
    Next project
    End Sub
    Last edited by arnelgp; 05-03-2022 at 06:28 AM.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    In personal.xlsb:

    Sub M_snb()
      activesheet.cells.replace inputbox("search"), inputbox("replace by")
    End Sub
    PS. Your level of VBA expertise doesn't match the code you are working with.

  4. #4
    Thanks both

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •