VBA (Excel) Initialize Entire Array without Looping

后端 未结 6 498
有刺的猬
有刺的猬 2020-12-07 16:10

I am fairly new to VBA, so this may be a simple question but here goes.

I would like to initialize an entire array myArray, say of integers, in VBA. I k

6条回答
  •  無奈伤痛
    2020-12-07 17:04

    You can initialize the array by specifying the dimensions. For example

    Dim myArray(10) As Integer
    Dim myArray(1 to 10) As Integer
    

    If you are working with arrays and if this is your first time then I would recommend visiting Chip Pearson's WEBSITE.

    What does this initialize to? For example, what if I want to initialize the entire array to 13?

    When you want to initailize the array of 13 elements then you can do it in two ways

    Dim myArray(12) As Integer
    Dim myArray(1 to 13) As Integer
    

    In the first the lower bound of the array would start with 0 so you can store 13 elements in array. For example

    myArray(0) = 1
    myArray(1) = 2
    '
    '
    '
    myArray(12) = 13
    

    In the second example you have specified the lower bounds as 1 so your array starts with 1 and can again store 13 values

    myArray(1) = 1
    myArray(2) = 2
    '
    '
    '
    myArray(13) = 13
    

    Wnen you initialize an array using any of the above methods, the value of each element in the array is equal to 0. To check that try this code.

    Sub Sample()
        Dim myArray(12) As Integer
        Dim i As Integer
    
        For i = LBound(myArray) To UBound(myArray)
            Debug.Print myArray(i)
        Next i
    End Sub
    

    or

    Sub Sample()
        Dim myArray(1 to 13) As Integer
        Dim i As Integer
    
        For i = LBound(myArray) To UBound(myArray)
            Debug.Print myArray(i)
        Next i
    End Sub
    

    FOLLOWUP FROM COMMENTS

    So, in this example every value would be 13. So if I had an array Dim myArray(300) As Integer, all 300 elements would hold the value 13

    Like I mentioned, AFAIK, there is no direct way of achieving what you want. Having said that here is one way which uses worksheet function Rept to create a repetitive string of 13's. Once we have that string, we can use SPLIT using "," as a delimiter. But note this creates a variant array but can be used in calculations.

    Note also, that in the following examples myArray will actually hold 301 values of which the last one is empty - you would have to account for that by additionally initializing this value or removing the last "," from sNum before the Split operation.

    Sub Sample()
        Dim sNum As String
        Dim i As Integer
        Dim myArray
    
        '~~> Create a string with 13 three hundred times separated by comma
        '~~> 13,13,13,13...13,13 (300 times)
        sNum = WorksheetFunction.Rept("13,", 300)
        sNum = Left(sNum, Len(sNum) - 1)
    
        myArray = Split(sNum, ",")
    
        For i = LBound(myArray) To UBound(myArray)
            Debug.Print myArray(i)
        Next i
    End Sub
    

    Using the variant array in calculations

    Sub Sample()
        Dim sNum As String
        Dim i As Integer
        Dim myArray
    
        '~~> Create a string with 13 three hundred times separated by comma
        sNum = WorksheetFunction.Rept("13,", 300)
        sNum = Left(sNum, Len(sNum) - 1)
    
        myArray = Split(sNum, ",")
    
        For i = LBound(myArray) To UBound(myArray)
            Debug.Print Val(myArray(i)) + Val(myArray(i))
        Next i
    End Sub
    

提交回复
热议问题