我了解到,在提高微距速度方面,As Long 比 As Variant 好得多。
Dim data1() As Variant
data1 = Array("a", "b", "c", "d", "e")
Sheet1.ListBox1.List = data1
当我用 As Long 替换时,我的代码不再起作用。
我了解到,在提高微距速度方面,As Long 比 As Variant 好得多。
Dim data1() As Variant
data1 = Array("a", "b", "c", "d", "e")
Sheet1.ListBox1.List = data1
当我用 As Long 替换时,我的代码不再起作用。
您的数组不包含Longs,它包含Strings。试试Dim data1() As String。(别担心 - 它可能仍然比As Variant.)
虽然使用显式类型通常(几乎总是)更好。这种情况是一个例外,因为 的List属性无论如何ListBox都是 a Variant array,因此将 aString或Long数组分配给它会导致隐式类型转换,因此不会更快(甚至可能更慢)。
可能会有所不同的是,在将数据分配给ListBox. 如果使用显式类型数组更有效,那就使用它。
顺便提一句
Dim data1() as long
data1 = Array(1, 2, 3, 4, 5)
不起作用,因为Array(...)返回Varaint array不能分配给显式类型数组的 a。