Dim myChar As Char
抛出编译错误: “未定义用户定义类型”
要使用 Char 类型,我应该包含哪些引用信息?
最佳答案
VBA 中不存在Char
类型,您应该使用String
代替。
Dim myChar As String
请注意,VBA 与 VB.NET 不同。在 VB.NET 中,您可以使用 Char
。
编辑:按照 Michał Krzych 的建议,我将使用固定长度字符串来实现此特定目的。
Dim myChar As String * 1
这是 "VBA Developer's Handbook" by Ken Getz and Mike Gilbert 的摘录:
"Dynamic strings require a bit more processing effort from VBA and are, accordingly, a bit slower to use." ... "When working with a single character at a time, it makes sense to use a fixed-length string declared to contain a single character. Because you know you'll always have only a single character in the string, you'll never need to trim off excess space. You get the benefits of a fixed-length string without the extra overhead."
需要注意的是,函数和子例程中不允许使用固定字符串表示法;
Sub foo (char as string *1) '不允许
...
结束子
所以你要么需要使用;
Sub foo (char as string) '这将允许任何字符串通过
或者
Sub foo (char as byte) '字符串需要转换为工作
使用字节时需要注意的一点是没有标准 字节是否无符号。 VBA 使用无符号字节,这 在这种情况下很方便。
关于vba - 未定义 Char 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28623732/