vba - 将字符串数组分配给类属性

标签 vba excel

我很难正确设置和调用字符串数组类属性。

当前类属性

Private pNames() As String

'pNames Properties
Public Property Get Names() As String
    Names() = pNames
End Property
Public Property Let Names(Names As String)
    pNames() = Names
End Property

当前编译错误

Compile Error: Expected array

在下面一行,特别突出显示LBound:

For Loop1 = LBound(EditCategories.Names) To UBound(EditCategories.Names)

如何将此属性正确注册为字符串数组?

<小时/>

我尝试过的

Public Property Let Names()(Names As String) 突出显示空括号并抛出:

Argument required for Property Let or Property Set

Public Property Let Names(Names() As String) 突出显示整行并抛出:

Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalid Set final parameter

最佳答案

支持字段是一个String数组。类型化数组的 VBA 语法有点奇怪/不一致。

支持字段:

Private pNames() As String

属性:

Public Property Get Names() As String()

注意 String() 括号 - 这在 Private pNames 声明中是非法的 - 想想看。

问题是您无法分配给这样的数组,因此 Property Let 访问器不可能合法 - 这不会编译:

Public Property Let Names(ByRef values As String())

欢迎体验 VBA 中类型化数组的乐趣。只需将其公开为 Variant 并完成即可:

Public Property Get Names() As Variant
Public Property Let Names(ByRef values As Variant)

关于vba - 将字符串数组分配给类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45020999/

相关文章:

sql-server - SSIS 到 Excel - Excel 公式没有自动运行?

javascript - 创建工作簿时未定义不是函数

VBA 单元格突出显示

php - 使用 Excel 根据 4 级类别创建零件编号,每个类别都具有基于类别组合的唯一标识符

excel - 调用将 dict 作为值传递的子过程时,参数不可选

excel - 如何从 VBA 在 Excel 2010 中创建安全受密码保护的 .xls 文件

excel - 遍历文件夹并复制第一张工作表的值和格式

excel - 将excel文件转换成prn给出不同的语言数据

excel - 取消 = True 在 Workbook_BeforeClose 中不起作用

excel - Excel 中缺少小时数