Replace variables in Module1

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Replace variables in Module1

Post by YasserKhalil »

I have some codes in Module1. I need first to copy all the codes to another module and rename the new module [NewModule].
Module1 has a lot of words that starts with the letters `ll` and I need to replace all these variables with descriptive words.
Say the new words should be Array("aa", "bb", "cc", "ko", "lm", "op", "st") .. or the new words should be a1, a2, a3 and so on.

I need regex pattern that extracts all the letters of subsequent of letters `L` & `I` (small or capital) with more than 10 in length.

Code: Select all

Option Explicit

Private Const lllIIlIIIIlIllIllIllIIIIIIIIllIlIIIIIIII As Long = 1000
Public Function ASAPSUMBYFONTCOLOR(LookRange As Range, color_index_nr As Variant) As Double
Dim Boolean1 As Boolean, Double1 As Double, Range1 As Range, ar As Variant, ar2 As Variant
1 If Len(llIIllIIlllIlllIllIlIIIIIIIlIllIlIIIIIII) = 0 Then
2 llIIIIIIlIIlIllIllIIlIIlIIllIlIIlIIIIIII (llIllIlIIIlIllIIllIlllIlIllIIllIlIIIIIII)
3 End If
4 On Error GoTo llIIIllIIlIllIIIIIlIIlIIIIIIllIlIIIIIIII
5 Double1 = 0
6 If UCase(TypeName(color_index_nr)) = llIIllIIlllIlllIllIlIIIIIIIlIllIlIIIIIII Then
7 ar2 = llIIlIIIllIlllllIllIIIIllllIIIIIlIIIIIII(color_index_nr, 3)
8 Boolean1 = True
9 Else
10 ar2 = color_index_nr
11 End If
12 If LookRange.Cells.Count > lllIIlIIIIlIllIllIllIIIIIIIIllIlIIIIIIII Then
13 Set LookRange = Application.Intersect(LookRange, LookRange.Parent.UsedRange)
14 End If
15 If Boolean1 Then
16 For Each Range1 In LookRange.Cells
17 If llIIlIIIllIlllllIllIIIIllllIIIIIlIIIIIII(Range1, 3) = ar2 Then
18 ar = Range1.Value2
19 If IsNumeric(ar) And Not lIIIIlllllIIlIlIlIlllIlIIllIIlIIlIIIIIII(ar) Then Double1 = Double1 + ar
20 End If
21 Next
22 Else
23 For Each Range1 In LookRange.Cells
24 If llIIlIIIllIlllllIllIIIIllllIIIIIlIIIIIII(Range1, 4) = ar2 Then
25 ar = Range1.Value2
26 If IsNumeric(ar) And Not lIIIIlllllIIlIlIlIlllIlIIllIIlIIlIIIIIII(ar) Then Double1 = Double1 + ar
27 End If
28 Next
29 End If
30 ASAPSUMBYFONTCOLOR = Double1
31 Exit Function
llIIIllIIlIllIIIIIlIIlIIIIIIllIlIIIIIIII:
32 If llIIllIIIlIlIlIlIIIIIIIllIIlIllIlIIIIIII Then
33 ASAPSUMBYFONTCOLOR = CVErr(xlErrName)
34 Else
35 ASAPSUMBYFONTCOLOR = CVErr(xlErrNA)
36 End If
End Function

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Replace variables in Module1

Post by YasserKhalil »

Here's the final solution

Code: Select all

Sub Test()
    Dim x, m, txt As String, i As Long, iLine As Long
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    With ThisWorkbook.VBProject.VBComponents("Module1").codeModule
        For iLine = 1 To .CountOfLines
            txt = txt & .Lines(iLine, 1) & vbCrLf
        Next iLine
    End With
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[LIli]{10,}"
        For Each m In .Execute(txt)
            If Not dict.exists(m.Value) Then
                dict.Add m.Value, "a" & dict.Count + 1
            End If
            txt = Replace(txt, m.Value, dict(m.Value))
        Next m
    End With
    x = dict.Items
    Range("A1").Resize(UBound(x) + 1).Value = Application.Transpose(x)
    With ThisWorkbook.VBProject.VBComponents("Module1").codeModule
        .DeleteLines 1, .CountOfLines
        .InsertLines 1, txt
    End With
End Sub