Is it possible in Excel VBA to change the source code of Module in another Module

前端 未结 3 915
心在旅途
心在旅途 2020-12-06 14:41

I have an Excel .xlam file that adds a button in the ribbon to do the following:

  1. Scan the ActiveSheet for some pre-set parameters
  2. Take my source text
3条回答
  •  余生分开走
    2020-12-06 15:15

    I realize now that what you really want to do is store some values in your document in a way that is accessible to your VBA, but that is not readable to a user of the spreadsheet. Following Charles Williams's suggestion to store the value in a named range in a worksheet, and addressing your concern that you don't want the user to have access to the values, you would have to encrypt the string...

    The "proper way" to do this is described in this article - but it's quite a bit of work.

    A much shorter routine is found here. It just uses simple XOR encryption with a hard coded key - but it should be enough for "most purposes". The key would be "hidden" in your macro, and therefore not accessible to prying eyes (well, not easily).

    Now you can use this function, let's call it encrypt(string), to convert your string to a value in the spreadsheet:

    range("mySecretCell").value = encrypt("The lazy dog jumped over the fox")
    

    and when you need to use it, you use

    Public Function GetSource()
        GetSource = decrypt(Range("mySecretCell").value)
    End Function
    

    If you use the XOR version (second link), encrypt and decrypt would be the same function...

    Does that meet your needs better?

提交回复
热议问题