UDF中的Excel UDF产生#VALUE错误


1

首先,感谢nixda的哈希码!

我是VBA的新手。我正在尝试创建一个接受电子邮件地址并混淆用户名部分的函数,以便可以分发包含混淆后的电子邮件地址的报告。

我将以下内容放入VBA模块中:

Private Function EncodeBase64(ByVal sTextToHash As String)

Dim objXML As Object
Dim objNode As Object

Set objXML = CreateObject("MSXML2.DOMDocument")
Set objNode = objXML.createElement("b64")

objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.Text

Set objNode = Nothing
Set objXML = Nothing

End Function

Public Function BASE64SHA1(ByVal sTextToHash As String)

Dim asc As Object
Dim enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Dim bytes() As Byte
Const cutoff As Integer = 5

Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")

TextToHash = asc.GetBytes_4(sTextToHash)
SharedSecretKey = asc.GetBytes_4(sTextToHash)
enc.Key = SharedSecretKey

bytes = enc.ComputeHash_2((TextToHash))
BASE64SHA1 = EncodeBase64(bytes)
BASE64SHA1 = Left(BASE64SHA1, cutoff)

Set asc = Nothing
Set enc = Nothing

End Function

如果我在工作簿中调用该函数,则可以混淆电子邮件地址:

=BASE64SHA1(LEFT(A1,FIND("@",A1)-1))&"@"&MID(A1,FIND("@",A1)+1,999)

因此,如果我通过此功能,我将获得一个带有哈希用户名的电子邮件地址。大!

但我想输入:

=OBFUSCATEEMAIL(A1)

在工作表中

因此,我打开了工作表的VBA模块,到目前为止,我已经:

Public Function ObfuscateEmail(ByVal email As String)

    Dim username As String
    Dim domain As String

    username = Left(email, InStr(email, "@") - 1)
    domain = Right(email, Len(email) - InStr(email, "@"))

    ObfuscateEmail = BASE64SHA1(username) & "@" & domain

End Function

当我尝试调用此新函数时,出现#VALUE错误!

对BASE64SHA1的调用导致了此问题,但是:

a)从工作表中调用时,我知道BASE64SHA1正在工作,并且

b)我将ObfuscateEmail正常工作,直到将调用添加到BASE64SHA1

我究竟做错了什么?

Answers:


0

这行代码:

objNode.nodeTypedValue = arrData

使用既不是Dim'ed也未分配值的变量。


那么,为什么从工作簿(通过BASE64SHA1)调用时,该行会导致零问题,而在模块内部调用时,会导致问题呢?
大卫·弗农

1
糟糕的是,在路上的某个地方搞砸了EncodeBase64的标头,应该是Private Function EncodeBase64(ByRef arrData() As Byte) As String。这样可以解决您指出的问题。。谢谢!
大卫·弗农

@DavidVernon调试很好!
加里的学生
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.