title: Find and replace all names of variables in VBA module
body: <p>Let's assume that we have one module with only one Sub in it, and there are no comments. How to identify all variable names ? Is it possible to identify names of variables which are not defined using <code>Dim</code> ? I would like to identify them and replace each with some random name to obfuscate my code (O0011011010100101 for example), replace part is much easier.</p>
  
  <p>List of characters which could be use in names of macros, functions and variables :</p>
  
  <pre><code>ABCDEFGHIJKLMNOPQRSTUVWXYZdefghijklmnopqrstuvwxyzg€‚„…†‡‰Š‹ŚŤŽŹ‘’“”•–—™š›śťžź ˇ˘Ł¤Ą¦§¨©Ş«¬­®Ż°±˛ł´µ¶·¸ąş»Ľ˝ľżŔÁÂĂÄĹĆÇČÉĘËĚÍÎĎĐŃŇÓÔŐÖ×ŘŮÚŰÜÝŢßŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙ÉĘËĚÍÎĎĐŃŇÓÔŐÖ×ŘŮÚŰÜÝŢßŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙
  </code></pre>
  
  <p>Below are my function I've wrote recenlty :</p>
  
  <pre><code>Function randomName(n as integer) as string
     y="O"
     For i = 2 To n:
         If Rnd() &gt; 0.5 Then
            y = y &amp; "0"
         Else
            y = y &amp; "1"
         End If
      Next i
  
     randomName=y
  End Function
  </code></pre>
  
  <p>In goal to replace given strings in another string which represent the code of module I use below sub :</p>
  
  <pre><code>Sub substituteNames()
      'count lines in "Module1" which is part of current workbook
      linesCount = ActiveWorkbook.VBProject.VBComponents("Module1").CodeModule.CountOfLines
      'read code from module
      code = ActiveWorkbook.VBProject.VBComponents("Module1").CodeModule.Lines(StartLine:=1, Count:=linesCount)
  
      inputStr = Array("name1", "name2", "name2") 'some hardwritten array with string to replace
      namesLength = 20                            'length of new variables names
  
      For i = LBound(inputStr) To UBound(inputStr)
         outputString = randomName(namesLength-1)
         code = Replace(code, inputStr(i), outputString)
      Next i
  
      Debug.Print code 'view code
  End Sub
  </code></pre>
  
  <p>then we simply substitute old code with new one, but how to identify strings with names of variables ?</p>
  
  <p><strong>Edition</strong>
  Using **Option Explicit ** decrease safety of my simple method of obfuscation, because to reverse changes you only have to follow <code>Dim</code> statements and replace ugly names with something normal. Except that to make such substitution harder, I think it's good idea to break the line in the middle of variable name :</p>
  
  <pre><code>O0O000O0OO0O0000 _
  0O00000O0OO0
  </code></pre>
  
  <p>the simple method is also replacing some strings with chains based on <code>chr</code> functions <code>chr(104)&amp;chr(101)&amp;chr(108)&amp;chr(108)&amp;chr(111)</code>  :</p>
  
  <pre><code>Sub stringIntoChrChain()
  
      strInput = "hello"
      strOutput = ""
  
      For i = 1 To Len(strInput)
          strOutput = strOutput &amp; "chr(" &amp; Asc(Mid(strInput, i, 1)) &amp; ")&amp;"
      Next i
  
      Debug.Print Mid(strOutput, 1, Len(strOutput) - 1)
  End Sub
  </code></pre>
  
  <p>comments like below could make impression on user and make him think that he does not poses right tool to deal with macro etc.:</p>
  
  <pre><code>'(k=Äó¬)w}ż^¦ů‡ÜOyúm=ěËnóÚŽb W™ÄQó’ (—*-ĹTIäb
  'R“ąNPÔKZMţ†üÍQ‡
  'y6ű˛Š˛ŁŽ¬=iýQ|˛^˙  ‡ńb ¬ĂÇr'ń‡e˘źäžŇ/âéç;1qýěĂj$&amp;E!V?¶ßšÍ´cĆ$Âű×şŮî’ď˛¦Ŕ?TáÄu[nG¦•¸î»éüĽ˙xVPĚ.|
  'ÖĚ/łó®Üă9Ę]ż/ĹÍT¶Mµę¶mÍ
  'q[—qëýY~Pc©=jÍ8˘‡,Ú+ń8ŐűŻEüńWü1ďëÇ±†ć}ęńwŠbŢ,&gt;ó’Űçµ™Š_…qÝăt±+‡ĽČg­řÍ!·eŠP âńđ:Ĺ¶OĂˇžű?őë®ÁšńýĎáËTbž}|Ö…ăË[®™
  </code></pre>