-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcelMacroSimplifier.html
More file actions
359 lines (317 loc) · 14.6 KB
/
excelMacroSimplifier.html
File metadata and controls
359 lines (317 loc) · 14.6 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel Macro Simplifier</title>
<style>
body {
background-color: lightblue;
margin: 10vh;
}
p{
border-style: solid;
border-width: 3px;
border-color: silver;
background-color: lightcyan;
padding: 10px;
border-radius: 4px;
width: 80vw;
}
pre{
margin: 0
}
button{
font-size:16px;
margin:10px;
height:30px;
width:200px;
border-radius: 4px;
color: white;
background:navy;
cursor: pointer;
border: none
}
button:hover{
background-color: blue;
}
textarea{
font-size:16px;
overflow: auto;
resize: vertical;
height: 300px;
width: 80vw;
max-height: 700px;
min-height: 200px;
border-radius: 4px;
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>Excel Macro Simplifier</h1>
</header>
<div class="content">
<div class="panel">
<h2>Input Macro</h2>
<textarea id="inputMacro" placeholder="Paste your recorded Excel VBA macro here..."></textarea>
</div>
<div class="button-container">
<button class="btn-simplify" onclick="simplifyMacro()">Simplify Macro</button>
<button class="btn-copy" onclick="copyOutput()">Copy Output</button>
</div>
<div class="panel">
<h2>Simplified Output</h2>
<textarea id="outputMacro" readonly placeholder="Simplified macro will appear here..."></textarea>
</div>
<div class="info">
<p class="subtitle">Remove unnecessary actions and make your VBA code cleaner</p>
<h3>What gets removed:</h3>
<ul>
<li>Unnecessary <code>Range().Select</code> and <code>Selection</code> statements</li>
<li><code>ActiveCell</code> references when direct assignment is possible</li>
<li><code>Application.CutCopyMode</code> changes</li>
<li><code>ActiveWindow.Zoom</code> changes</li>
<li><code>ActiveWindow.SmallScroll</code> and <code>ActiveWindow.LargeScroll</code> actions</li>
<li>Range selections that aren't used in subsequent operations</li>
</ul>
</div>
</div>
</div>
<script>
function simplifyMacro() {
const input = document.getElementById('inputMacro').value;
const output = document.getElementById('outputMacro');
if (!input.trim()) {
alert('Please paste a macro first!');
return;
}
const simplified = processVBACode(input);
output.value = simplified;
}
function processVBACode(code) {
// First, combine lines that end with continuation character "_"
const rawLines = code.split('\n');
const combinedLines = [];
let i = 0;
while (i < rawLines.length) {
let currentLine = rawLines[i];
// Check if line ends with continuation character
while (i < rawLines.length - 1 && currentLine.trimEnd().endsWith('_')) {
// Remove the trailing _ and whitespace, then append next line
currentLine = currentLine.trimEnd().slice(0, -1).trimEnd() + ' ' + rawLines[i + 1].trim();
i++;
}
combinedLines.push(currentLine);
i++;
}
const lines = combinedLines;
const result = [];
i = 0;
let baseRange = null; // The initial range (e.g., "B5")
let endChain = []; // Chain of .End() calls
let isRangeExpansion = false; // Whether we're building a Range(x, y) expansion
let rangeEndChain = []; // Chain for the second part of Range(Selection, Selection.End()...)
while (i < lines.length) {
const line = lines[i];
const trimmed = line.trim();
// Keep Sub declarations, empty lines, comments, End With, and property lines inside With blocks
if (trimmed.startsWith('Sub ') || trimmed.startsWith('End Sub') || trimmed === '' ||
trimmed.startsWith("'") || trimmed === 'End With' || trimmed.startsWith('.')) {
result.push(line);
i++;
continue;
}
// Skip useless actions
if (isUselessAction(trimmed)) {
i++;
continue;
}
// Handle Range().Select - this sets the base range
if (trimmed.match(/^Range\([^)]+\)\.Select$/)) {
const rangeMatch = trimmed.match(/^Range\(([^)]+)\)\.Select$/);
if (rangeMatch) {
baseRange = rangeMatch[1];
endChain = [];
isRangeExpansion = false;
rangeEndChain = [];
}
i++;
continue;
}
// Handle Selection.End(xlDirection).Select - accumulates End() calls
if (trimmed.match(/^Selection\.End\(xl(Up|Down|ToLeft|ToRight)\)\.Select$/)) {
const direction = trimmed.match(/^Selection\.End\((xl(?:Up|Down|ToLeft|ToRight))\)\.Select$/)[1];
if (baseRange) {
endChain.push(direction);
}
i++;
continue;
}
// Handle Range(Selection, Selection.End(xlDirection)).Select - range expansion
if (trimmed.match(/^Range\(Selection,\s*Selection\.End\(xl(Up|Down|ToLeft|ToRight)\)\)\.Select$/)) {
const direction = trimmed.match(/^Range\(Selection,\s*Selection\.End\((xl(?:Up|Down|ToLeft|ToRight))\)\)\.Select$/)[1];
if (baseRange) {
isRangeExpansion = true;
rangeEndChain.push(direction);
}
i++;
continue;
}
// Handle With Selection blocks
if (baseRange && trimmed.startsWith('With Selection')) {
const simplifiedEndChain = simplifyDirections(endChain);
const simplifiedRangeEndChain = simplifyDirections(rangeEndChain);
let finalRange;
if (isRangeExpansion && simplifiedRangeEndChain.length > 0) {
let endPart = baseRange;
for (const dir of simplifiedRangeEndChain) {
endPart = `${endPart}.End(${dir})`;
}
finalRange = `Range(${baseRange}, ${endPart})`;
} else if (isRangeExpansion && simplifiedRangeEndChain.length === 0) {
finalRange = `Range(${baseRange})`;
} else {
finalRange = `Range(${baseRange})`;
}
const replacement = trimmed.replace('With Selection', `With ${finalRange}`);
result.push(line.replace(trimmed, replacement));
i++;
continue;
}
// Now we hit an actual operation - build the final range expression
if (baseRange && (trimmed.startsWith('ActiveCell.') || trimmed.startsWith('Selection.') || trimmed.startsWith('ActiveSheet.'))) {
// Simplify opposite directions that cancel out
const simplifiedEndChain = simplifyDirections(endChain);
const simplifiedRangeEndChain = simplifyDirections(rangeEndChain);
let finalRange;
let needsValidation = false;
if (isRangeExpansion && simplifiedRangeEndChain.length > 0) {
// Build Range(base, base.End()...) format
let endPart = baseRange;
for (const dir of simplifiedRangeEndChain) {
endPart = `${endPart}.End(${dir})`;
}
finalRange = `Range(${baseRange}, ${endPart})`;
needsValidation = true;
} else if (isRangeExpansion && simplifiedRangeEndChain.length === 0) {
// Directions cancelled out completely
finalRange = `Range(${baseRange})`;
needsValidation = true;
} else if (simplifiedEndChain.length > 0) {
// Build base.End()...End().Select format - keep as selection
let rangeExpr = `Range(${baseRange})`;
for (const dir of simplifiedEndChain) {
rangeExpr += `.End(${dir})`;
}
// Output the Select statement before operations
result.push(line.replace(trimmed, `${rangeExpr}.Select`));
// Reset and process current line as ActiveCell operation
baseRange = null;
endChain = [];
isRangeExpansion = false;
rangeEndChain = [];
// Keep the current operation line as-is
result.push(line);
i++;
continue;
} else {
// Simple range reference
finalRange = `Range(${baseRange})`;
}
// Replace the operation
let outputLine = '';
if (trimmed.startsWith('ActiveCell.FormulaR1C1')) {
const value = trimmed.match(/ActiveCell\.FormulaR1C1\s*=\s*(.+)/);
if (value) {
outputLine = line.replace(trimmed, `${finalRange}.FormulaR1C1 = ${value[1]}`);
}
} else if (trimmed.startsWith('Selection.FormulaR1C1')) {
const value = trimmed.match(/Selection\.FormulaR1C1\s*=\s*(.+)/);
if (value) {
outputLine = line.replace(trimmed, `${finalRange}.FormulaR1C1 = ${value[1]}`);
}
} else if (trimmed.startsWith('Selection.NumberFormat')) {
const value = trimmed.match(/Selection\.NumberFormat\s*=\s*(.+)/);
if (value) {
outputLine = line.replace(trimmed, `${finalRange}.NumberFormat = ${value[1]}`);
}
} else if (trimmed === 'Selection.Copy') {
outputLine = line.replace(trimmed, `${finalRange}.Copy`);
} else if (trimmed === 'Selection.Paste' || trimmed === 'ActiveSheet.Paste') {
outputLine = line.replace(trimmed, `${finalRange}.Paste`);
} else if (trimmed === 'Selection.FillDown') {
outputLine = line.replace(trimmed, `${finalRange}.FillDown`);
} else if (trimmed.startsWith('Selection')) {
const params = trimmed.substring('Selection'.length);
outputLine = line.replace(trimmed, `${finalRange}${params}`);
} else {
outputLine = line;
}
// Add validation comment if needed
if (needsValidation && outputLine) {
outputLine = outputLine + " 'please validate the range used";
}
if (outputLine) {
result.push(outputLine);
}
i++;
continue;
}
// Handle standalone ActiveSheet.Paste without active range (skip it)
if (trimmed === 'ActiveSheet.Paste') {
i++;
continue;
}
// Keep other lines
if (trimmed.length > 0) {
result.push(line);
}
i++;
}
return result.join('\n');
}
// Simplify direction chains by canceling opposite directions
function simplifyDirections(directions) {
const opposites = {
'xlUp': 'xlDown',
'xlDown': 'xlUp',
'xlToLeft': 'xlToRight',
'xlToRight': 'xlToLeft'
};
const result = [];
for (const dir of directions) {
const lastDir = result[result.length - 1];
if (lastDir && opposites[lastDir] === dir) {
// Cancel out opposite directions
result.pop();
} else {
result.push(dir);
}
}
return result;
}
function isUselessAction(line) {
const uselessPatterns = [
/^Application\.CutCopyMode/,
/^ActiveWindow\.Zoom/,
/^ActiveWindow\.SmallScroll/,
/^ActiveWindow\.LargeScroll/,
];
return uselessPatterns.some(pattern => pattern.test(line));
}
function copyOutput() {
const output = document.getElementById('outputMacro');
if (!output.value.trim()) {
alert('Nothing to copy! Simplify a macro first.');
return;
}
output.select();
document.execCommand('copy');
alert('Copied to clipboard!');
}
</script>
</body>
</html>