哪些Excel对象是基于零的对象,哪些是基于1的对象?


20

使用VBA访问工作表中的第一张表是Worksheets(1)。ListBox中的第一项是myListBox.List(0)。我听说Collections是基于1的,但我不知道它们是什么。VBA阵列基于0。Excel字符串函数(例如MID)基于1。是否有基于0或1的通用原则,或者您可以提供每个列表?


cell(1,1).characters(index,length)是基于1的,但会进行某种边界裁剪,因此cell(1,1).characters(0,length)= cell(1,1).characters(1,长度)(excel 2013)
seanv507

Answers:


24

VBA中有3种主要的分组构造类型,索引之间有所区别

  • 集合-从1开始的索引

    • 基于0的例外:UserForm集合,例如选项卡,页面,控件(列表框,文本框)
    • 集合是本机Excel对象,包含逻辑相关对象的组(或列表)
    • 通常用于容纳复杂对象,但也可以容纳基本类型
    • Excel集合:

      • 工作簿,图纸,范围,形状
      • Sheets(1)是文件中的第一个单元格,Cells(1,1)是第一行和第一列中的单元格
    • 集合的主要优点是方便按名称访问元素

      • For-Each循环非常高效(与数组的For-Each处理相比)
      • 但是,按索引访问单个项目要比按名称访问它们快

  • 数组-默认为0,但是第一个索引可以更改为任意数字(如下所示)

    • 数组是包含一组相关变量的变量
    • 通常用于基本数据类型,例如布尔,整数,长整数,字符串,双精度等
    • 一旦定义,它将仅容纳一种类型的项目: Dim x() As Long

      • 为了容纳更复杂的对象,可以将数组定义为 Dim x() As Variant
      • 变体可以是任何类型的对象,包括工作簿,图纸,范围,数组

        • Dim x As Variant: x = Array(1) '1 Variant variable containing 1 array
        • Dim y(2) As Variant '1 Variant array containing 3 arrays
        • y(0) = Array(1): y(1) = Array(2): y(2) = Array(3)
    • 数组的主要优点是按索引访问项目时的性能

      • For index=0 To 10循环比For-Each循环快

  • 字典-未建立索引,但可以使用键模拟索引

    • VB脚本的本机而不是VBA的本机(必须使用外部库)
    • 可以容纳任何类型的对象,包括数组,集合或其他字典

ListBox是一个复杂的对象,可以通过基于0的控件集合进行访问

ListBox的.List()属性是一个从0开始的数组

其他注意事项

  • 从0开始的索引是其他语言的标准

  • VBA引入了基于1的概念,以使新用户更加直观:

    • 从Sheet1到Sheet3,集合计数为3比使用起来容易
    • Sheet0到Sheet2,集合计数为3

关于它们之间的索引差异的一些实际示例:

Public Sub vbaCollections()
    Dim c As New Collection     '1-based index

    c.Add Item:="a", Key:="1"   'index 1; Key must a String
    c.Add Item:="b", Key:="2"   'index 2
    c.Add Item:="c", Key:="3"   'index 3

    Debug.Print c.Count         '3;   Items in index sequence: a,b,c, Keys: "1","2","3"
    Debug.Print c.Item(1)       'a;   not available for Dictionaries
    'Debug.Print c.Key("1")     'invalid, so is: c.Key(1)

    c.Remove Index:=2
    Debug.Print c.Count         '2;   items in index sequence: a,c, Keys: "1","3"
    'c.Remove Item:="c"         'invalid, so is: c.Remove Key:="3"

    'c.Add Item:="c", Key:="3", Before:=1   'Key must be unique - Error
    c.Add Item:="c", Key:="5", Before:=1    'allows duplicate Item
    Debug.Print c.Count         '3;   items in index sequence: c,a,c, Keys: "5","1","3"
End Sub

Public Sub vbaArrays()
    Dim a() As Long, b(3) As Long   'Arrays default to "Option Base {0 | 1}"
    Dim c(0 To 0)                   'if "Option Base" not defined, it defaults to 0
    Dim ar(1) As Worksheet: Set ar(0) = Worksheets(1)   'array with 1 Worksheets object

    ReDim a(3)          'creates an array of 4 elements; indexes 0,1,2,3
        Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a)   'LB: 0, UB: 3
        Debug.Print UBound(a) - LBound(a)                       '3, array b() is the same

    'even whith "Option Base 1", the following still default to 0
    Dim v As Variant:  v = Split("A B")         'array with 2 items: v(0) = "A", v(1) = "B"
    'UserForm1.ListBox1.List = Array("Test")    'array with 1 item: .List(0,0) = "Test"

    ReDim a(0 To 3)     'creates an array of 4 elements; indexes 0,1,2,3
    a(0) = 1:   a(1) = 2:   a(2) = 3    'a(3) defaults to 0

        Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a)   'LB: 0, UB: 3
        Debug.Print UBound(a) - LBound(a)                       '3; offset index by -1

    ReDim a(1 To 3)     'creates an array of 3 elements; indexes 1,2,3
    a(1) = 1:   a(2) = 2:   a(3) = 3

        Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a)   'LB: 1, UB: 3
        Debug.Print UBound(a) - LBound(a)                       '2; offset count by +1

    ReDim a(5 To 7)     'creates an array of 3 elements; indexes 5,6,7
    a(5) = 1:   a(6) = 2:   a(7) = 3

        Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a)   'LB: 5, UB: 7
        Debug.Print UBound(a) - LBound(a)                       '2; offset count by +1

    ReDim a(-3 To -1)   'creates an array of 3 elements; indexes -3,-2,-1
    a(-3) = 1:  a(-2) = 2:  a(-1) = 3

        Debug.Print "LB: " & LBound(a) & ", UB: " & UBound(a)   'LB: -3, UB: -1
        Debug.Print UBound(a) - LBound(a)                       '2; offset count by +1
End Sub

Public Sub vbsDictionaries()
    Dim d As Object         'not indexed (similar to linked lists)
    Set d = CreateObject("Scripting.Dictionary")    'native to VB Script, not VBA

    d.Add Key:="a", Item:=1 'index is based on Key (a, b, c)
    d.Add Key:="b", Item:=2
    d.Add Key:="c", Item:=3
    Debug.Print d.Count     '3; Keys: a,b,c, Items: 1,2,3

    Debug.Print d(1)        'output is empty ("") - adds new element: Key:="1", Item:=""
    Debug.Print d.Count     '4; Keys: a,b,c,1, Items: 1,2,3,Empty
    Debug.Print d("a")      '1
    Debug.Print d(1)        'output is Empty ("") from element with Key:="1"

    'd.Add Key:="b", Item:=2        'attempt to add existing element: Key:="b" - Error

    'd.Keys  - 0-based array (not available for Collections)
    'd.Items - 0-based array (not available for Collections)

    d.Remove d.Keys()(1)            'remove element Item:=2 (Key:="b")
        Debug.Print d.Count         '3; Keys: a,c,1, Items: 1,3,""
    d.Remove d.Items()(0)           'remove Items element 0 (Key:="1", Item:="")
        Debug.Print d.Count         '2; Keys: a,c, Items: 1,3
    d.Remove "c"                    'remove element Key:="c" (Item:=3)
        Debug.Print d.Count         '1; Keys: a, Items: 1

    d.Add Key:="c", Item:=3
        Debug.Print d.Count         '2; Keys: a,c, Items: 1,3

    'd.Remove d.Items()(0)          'invalid
    Debug.Print d.Items()(d.Count - 1)  '3
    d.Remove d.Keys()(d.Count - 1)  'remove last element; access last Key by Key
        Debug.Print d.Count         '1; Keys: a, Items: 1

    Debug.Print d.Exists("a")       'True (not available for Collections)
    Debug.Print d.Exists(2)         'False
End Sub

进一步阅读:

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.