-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBA_IDE.bas
109 lines (101 loc) · 4.14 KB
/
VBA_IDE.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
Public Function VBA_Module_ReplaceAll(ByVal ModuleName As String, ByVal StringToFind As String, _
ByVal NewString, Optional ByVal FindWholeWord = False, _
Optional ByVal MatchCase = False, Optional ByVal PatternSearch = False) As Long
'Replace matched text in a VBA IDE by Module Name
'This can be usefull to comment out debug lines and other task before programmatically compiling your code to something like an Access ACCDE file
Dim mdl As Module
Dim lSLine As Long
Dim lELine As Long
Dim lSCol As Long
Dim lECol As Long
Dim sLine As String
Dim lLineLen As Long
Dim lBefore As Long
Dim lAfter As Long
Dim sLeft As String
Dim sRight As String
Dim sNewLine As String
Dim TotalReplaced As Long
Dim found As Boolean
TotalReplaced = 0
If StringToFind <> NewString Then 'prevents forever lop
Set mdl = Modules(ModuleName)
Do
lSCol = 0
lELine = 0
lECol = 0
If mdl.Find(StringToFind, lSLine, lSCol, lELine, lECol, FindWholeWord, _
MatchCase, PatternSearch) = True Then
If IsMissing(NewString) = False Then
sLine = mdl.lines(lSLine, Abs(lELine - lSLine) + 1)
lLineLen = Len(sLine)
lBefore = lSCol - 1
lAfter = lLineLen - CInt(lECol - 1)
sLeft = Left$(sLine, lBefore)
sRight = Right$(sLine, lAfter)
sNewLine = sLeft & NewString & sRight
mdl.ReplaceLine lSLine, sNewLine
End If
TotalReplaced = TotalReplaced + 1
End If
lSLine = lELine
Loop While lELine > 0
End If
VBA_Modules_ReplaceAll = TotalReplaced
Set mdl = Nothing
End Function
Public Function VBA_Modules_ReplaceAll(ByVal StringToFind As String, _
ByVal NewString, Optional ByVal FindWholeWord = False, _
Optional ByVal MatchCase = False, Optional ByVal PatternSearch = False) as long
'Replace matched text in a VBA IDE for all modules
'This can be usefull to comment out debug lines and other task before programmatically compiling your code to something like an Access ACCDE file
'NOTE--this function should be placed in its own module. You should also change the exclusion module name seen in the code below to prevent this function from rewriting itself!
Dim mdl As Module
Dim lSLine As Long
Dim lELine As Long
Dim lSCol As Long
Dim lECol As Long
Dim sLine As String
Dim lLineLen As Long
Dim lBefore As Long
Dim lAfter As Long
Dim sLeft As String
Dim sRight As String
Dim sNewLine As String
Dim TotalReplaced As Long
Dim found As Boolean
TotalReplaced = 0
If StringToFind <> NewString Then 'prevents forever loop
Dim intIndex As Integer
Dim mods As Modules
Set mods = Application.Modules
For intIndex = 0 To mods.Count - 1
If mods(intIndex).Name <> "VBA_IDE" Then 'you should change this to whatever module you places this function into so the funciton doesn't rewrite itself!!
Set mdl = Modules(mods(intIndex).Name)
Do
lSCol = 0
lELine = 0
lECol = 0
If mdl.Find(StringToFind, lSLine, lSCol, lELine, lECol, FindWholeWord, _
MatchCase, PatternSearch) = True Then
If IsMissing(NewString) = False Then
sLine = mdl.lines(lSLine, Abs(lELine - lSLine) + 1)
lLineLen = Len(sLine)
lBefore = lSCol - 1
lAfter = lLineLen - CInt(lECol - 1)
sLeft = Left$(sLine, lBefore)
sRight = Right$(sLine, lAfter)
sNewLine = sLeft & NewString & sRight
mdl.ReplaceLine lSLine, sNewLine
End If
TotalReplaced = TotalReplaced + 1
End If
lSLine = lELine
Loop While lELine > 0
End If
Next
End If
VBA_Modules_ReplaceAll = TotalReplaced
Set mdl = Nothing
Set mods = Nothing
End Function