14

Here is what I have done so far.

Sub MP_division()

Worksheets(3).Activate    
Dim last_cell As Integer    
Dim platforms() As String    
Dim arr_size As Integer  

platforms = Split(Cells(2, 47), ", ")    
last_cell = Mid(Range("A1048576").End(xlUp).Address, 4)    
arr_size = len(platforms) // is there something like this?    
For x = 1 To last_cell    
    For y = 1 To arr_size 
        //do something        
    Next        
Next   
End Sub

My question is, how may I get the array size(arr_size) that is returned by the split function, so that I could use in my for loop? Thank you.

brettdj
  • 54,857
  • 16
  • 114
  • 177
Kentot
  • 512
  • 5
  • 10
  • 26

3 Answers3

17

Consider abound using LBound and Ubound:

Sub MP_division()
    Dim last_cell As Long
    Dim platforms() As String
    Dim x As Long, y As Integer

    With ThisWorkbook.Worksheets(3)
        platforms = Split(.Cells(2, 47), ", ")
        last_cell = .Cells(.Rows.Count, "A").End(xlUp).Row
        For x = 1 To last_cell
            For y = LBound(platforms) To UBound(platforms)
                'do something
            Next
        Next
    End With
End Sub

Btw, Split always returns zero-based array (starts from 0, but not from 1). Thats why I recomend you to use both, Ubound and Lbound.

One more thing, I've changed Dim last_cell As Integer to Dim last_cell As Long, since max value of Integer only 32767 and if the last row would be greater than 32767, you would get an error with Integer.

And avoid using Select/Active statements (it's about Worksheets(3).Activate)

Community
  • 1
  • 1
Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
6

VBA LBound and UBound return the first and the last array position, so the correct answer is:

size = UBound(myArray) - LBound(myArray) + 1
farope
  • 61
  • 1
  • 2
4
Dim first as integer
Dim last as integer
Dim arr() as string
Dim lengthOfArray as integer

'split your data and save it in arr'

first = LBound(arr)
last = UBound(arr)

lengthOfArray = last - first

msgbox lengthOfArray 'This will display the length of the array'
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34