Is there a way of deleting all code lines beginning with debug.print across all modules using VBA?

I'm sure it could be used in code similar to yours though,
Yes, I use regex in my apps too. But only for simple string patterns. Something like multi line codes is out of my capacity. That's why I went for loops.
 
Thanks for your help. :rolleyes:
Sorry about being facetious - I actually will help when I can get to my PC so I can test any code I suggest. Currently existing on my Andoid tablet whilst on holiday. Another week or so before we head for home.
 
Simple and effective
But if the program were transformed into mde/accde and the debug.print files were eliminated, could there be any benefit?

Size reduction, certainly. When you have a code module, it takes up room twice. Once for the text form of the code, once for the compiled pseudo-code that is kept in a BLOB (Binary Large OBject). If you removed "debug.print" and replaced each instance with the single quote mark, that is 10 bytes per replacement. With 5000 of them in a single project, that would probably account for at least 50 Kb for the text and an unknown amount for the compiled code. I'd say that was a good size reduction.
 
Thanks for your help. :rolleyes:

I asked Chatty (ChatGPT) what she thought about using RegEx...here is her response:

https://mail.google.com/mail/u/0/
Using regex to comment out Debug.Print lines — including those with continuations — is theoretically possible and even elegant in some scripting environments (like Python or PowerShell). However, VBA's native regex support is limited, and applying regex to multiline VBA code directly is tricky and error-prone due to:

🔻 Limitations of Regex in VBA for This Task
  1. Multiline Regex is Awkward:
    • The VBScript RegExp object doesn’t natively support matching across multiple lines easily.
    • Continuation lines (_) break regex flow unless preprocessed into full logical lines.
  2. Parsing Code Safely Is Hard:
    • Regex doesn't parse code syntax; it matches patterns.
    • If someone has Debug.Print inside a comment, string literal, or complicated expression, regex can produce false positives.
  3. VBIDE Line-by-Line Limitation:
    • You're typically reading code from the CodeModule.Lines, which returns code line-by-line — you'd have to reconstruct the entire module as a string just to use regex, then rewrite the whole module, which is riskier.

✅ Where Regex Can Help
If you're exporting the modules to plain text (e.g., .bas files), regex becomes much more practical:
Example Regex (in a language that supports multiline):
^\s*Debug\.Print.*(?:_\r?\n.*)*
Replacement Strategy
In a scripting language like PowerShell or Python:
  1. Use regex to find the Debug.Print blocks.
  2. Add ' to each line in the block.
  3. Save it back, and re-import if needed.

🧠 Summary
MethodProsCons
Regex in VBALightweight, pattern-basedClunky for multiline; prone to false matches
VBIDE line loop (previous script)Accurate, safe per lineSlightly more verbose, but precise
Regex outside VBA (e.g., PowerShell)Powerful, easy multiline handlingRequires exporting/importing modules

✅ Recommendation
For Access VBA, especially if you're modifying modules in-place, stick with the VBIDE approach. It's less elegant than regex, but far safer and better integrated with the Access environment.
https://mail.google.com/mail/u/0/

ACg8ocLXsp-El-CKBrjWnnBFWnmUjKccP8gBDf8PF0Erv5gBMB-gpkHb=s40-p

She even provided VBA code, which look suspiciously like @KitaYama's...
 

Users who are viewing this thread

Back
Top Bottom