vba - 未定义 Char 类型

标签 vba excel excel-2013

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/

相关文章:

excel - VBA 按用户范围选择排序

vba - 引用另一张纸时出现“Subscript out of range”错误

vba - 在 Excel 中打印行单元格值

vba - Excel VBA 更改复制的事件工作表的名称

Excel 2013 VBA : Subscript out of Range (Error 9)

excel - 打开 CSV 文件

regex - 将三个函数合并为一个函数,以删除特定字符和数字周围的所有可能的空格

arrays - 不区分大小写的正则表达式 - VBA

java - 如何用Java编写IF-ELSE语句

vba - 命名动态范围并将其转换为变量