当值为 null 时,访问自定义属性的值会出现“内存不足”错误

sig*_*gil 6 excel vba

我正在尝试在表中创建自定义属性,然后检索其值。当我不使用空字符串时,这很好,即"". 当我使用空字符串时,出现此错误:

Run-time error '7':
Out of memory
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

Sub proptest()

Dim cprop As CustomProperty
Dim sht As Worksheet

Set sht = ThisWorkbook.Sheets("control")
sht.CustomProperties.Add "path", ""

For Each cprop In ThisWorkbook.Sheets("control").CustomProperties
    If cprop.Name = "path" Then
        Debug.Print cprop.Value
    End If
Next

End Sub
Run Code Online (Sandbox Code Playgroud)

代码在 处失败Debug.Print cprop.value。我不应该能够将属性设置为""最初吗?

K_B*_*K_B 1

我认为从 Daniel Dusek 的评论和回答来看,很明显这是不可能的。该属性应至少有 1 个字符才有效,不允许使用空字符串,否则在调用 .Value 时会给出错误。

因此,您可以Add使用长度为 1 或以上的属性string,并Delete在没有实际值分配给它时再次使用该属性。