-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVBA_replace
More file actions
304 lines (198 loc) · 6.64 KB
/
VBA_replace
File metadata and controls
304 lines (198 loc) · 6.64 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Option Explicit
Const DEFAULT_DUPLICATE_CASE_INSENSITIVE As Boolean = True
Public Sub ColumnA_ToRowString_SplitToColumnB()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If lastRow < 1 Then
MsgBox "Column A appears empty.", vbExclamation
Exit Sub
End If
Application.ScreenUpdating = False
Application.StatusBar = "Reading data..."
Dim data As Variant
data = ws.Range("A1:A" & lastRow).value
Dim ignoreCase As Boolean
ignoreCase = DEFAULT_DUPLICATE_CASE_INSENSITIVE
If CountDuplicatesArray(data, ignoreCase) > 0 Then
MsgBox "Duplicates found in Column A.", vbExclamation
GoTo SafeExit
End If
' ==========================
' PARSER METHOD
' ==========================
Dim methodChoice As Variant
methodChoice = Application.InputBox( _
"Choose parser method:" & vbCrLf & _
"1 = Fixed MID extract" & vbCrLf & _
"2 = Regex replace", _
"Parser", Type:=1)
If methodChoice = False Then GoTo SafeExit
Dim useMid As Boolean
Dim useRegexReplace As Boolean
useMid = (methodChoice = 1)
useRegexReplace = (methodChoice = 2)
If Not (useMid Or useRegexReplace) Then
MsgBox "Invalid method.", vbExclamation
GoTo SafeExit
End If
' ==========================
' PARSER INPUTS
' ==========================
Dim startPos As Long
Dim width As Long
Dim rxPattern As String
Dim rxReplace As String
If useMid Then
startPos = Application.InputBox("Start position:", Type:=1)
width = Application.InputBox("Width:", Type:=1)
Else
rxPattern = Application.InputBox("What to search?:", Type:=2)
rxReplace = Application.InputBox("Replace on to...(stay clear if don`t need it) :", Type:=2)
End If
' ==========================
' SEPARATOR
' ==========================
Dim sep As String
sep = Application.InputBox("Separator (divider):", Type:=2)
' ==========================
' MAX CELL LENGTH
' ==========================
Dim maxLen As Long
maxLen = Application.InputBox("Max cell length (<=32767)(256 is recomended) ", Type:=1)
If maxLen > 32767 Then maxLen = 32767
' ==========================
' PROCESS DATA
' ==========================
Dim parts() As String
Dim partCount As Long
Dim i As Long
Dim txt As String
Dim parsed As String
For i = 1 To UBound(data, 1)
If i Mod 1000 = 0 Then
Application.StatusBar = "Processing row " & i
End If
txt = Trim(CStr(data(i, 1)))
If txt <> "" Then
parsed = ParseValue(txt, _
useMid, startPos, width, _
useRegexReplace, _
rxPattern, rxReplace)
If parsed <> "" Then
partCount = partCount + 1
ReDim Preserve parts(1 To partCount)
parts(partCount) = parsed
End If
End If
Next i
If partCount = 0 Then
MsgBox "No values produced after parsing.", vbExclamation
GoTo SafeExit
End If
Dim joined As String
joined = Join(parts, sep)
joined = CollapseSeparator(joined, sep)
ws.Range("C:C").ClearContents
WriteChunks ws, joined, sep, maxLen
MsgBox "Finished.", vbInformation
SafeExit:
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
' ==========================
' DUPLICATE CHECK
' ==========================
Private Function CountDuplicatesArray(data As Variant, ignoreCase As Boolean) As Long
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim i As Long
Dim key As String
For i = 1 To UBound(data, 1)
key = Trim(CStr(data(i, 1)))
If ignoreCase Then key = LCase(key)
If key <> "" Then
If dict.Exists(key) Then
CountDuplicatesArray = CountDuplicatesArray + 1
Else
dict.Add key, 1
End If
End If
Next i
End Function
' ==========================
' PARSER
' ==========================
Private Function ParseValue(txt As String, _
useMid As Boolean, _
startPos As Long, _
width As Long, _
useRegexReplace As Boolean, _
pattern As String, _
repl As String) As String
If useMid Then
If startPos > Len(txt) Then Exit Function
ParseValue = Mid(txt, startPos, width)
ElseIf useRegexReplace Then
ParseValue = RegexReplace(txt, pattern, repl)
Else
ParseValue = txt
End If
End Function
Private Function RegexReplace(txt As String, pattern As String, repl As String) As String
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.Global = True
re.pattern = pattern
RegexReplace = re.Replace(txt, repl)
End Function
' ==========================
' SEPARATOR CLEANUP
' ==========================
Private Function CollapseSeparator(s As String, sep As String) As String
If sep = "" Then
CollapseSeparator = s
Exit Function
End If
Do While InStr(s, sep & sep) > 0
s = Replace(s, sep & sep, sep)
Loop
If Left(s, Len(sep)) = sep Then s = Mid(s, Len(sep) + 1)
If Right(s, Len(sep)) = sep Then s = Left(s, Len(s) - Len(sep))
CollapseSeparator = s
End Function
' ==========================
' WRITE OUTPUT
' ==========================
Private Sub WriteChunks(ws As Worksheet, bigText As String, sep As String, maxLen As Long)
Dim rowOut As Long
rowOut = 1
Dim remain As String
remain = bigText
Do While Len(remain) > 0
Dim chunk As String
chunk = NextChunk(remain, sep, maxLen)
ws.Cells(rowOut, "C").value = chunk
rowOut = rowOut + 1
remain = Mid(remain, Len(chunk) + 1)
If Left(remain, Len(sep)) = sep Then
remain = Mid(remain, Len(sep) + 1)
End If
Loop
End Sub
Private Function NextChunk(s As String, sep As String, maxLen As Long) As String
If Len(s) <= maxLen Then
NextChunk = s
Exit Function
End If
Dim candidate As String
candidate = Left(s, maxLen)
Dim pos As Long
pos = InStrRev(candidate, sep)
If pos > 0 Then
NextChunk = Left(candidate, pos - 1)
Else
NextChunk = Left(s, maxLen)
End If
End Function