Results 1 to 9 of 9

Thread: array lbound ubound

  1. #1
    Hyperactive Member
    Join Date
    Sep 09
    Posts
    468

    Resolved array lbound ubound

    I have question,

    I known how to check myArr(x)
    Lbound(myArr)and Uboubd(myArr), result: 0, 2 "mean 0 To 2"


    but I want to check array(y) mean that "0 To 1", any method, please

    please!


    Code:
    ................x......y
    ReDim myArr(0 To 2, 0 To 1) 
    
    myArr(0, 0) = "00"
    myArr(0, 1) = "01"
    myArr(1, 0) = "10"
    myArr(1, 1) = "11"
    myArr(2, 0) = "20"
    myArr(2, 1) = "21"
    Last edited by rpool; Sep 7th, 2012 at 08:35 PM.

  2. #2
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,255

    Re: array lbound ubound

    Provide the dimension you want to get its UBound, e.g. Debug.Print UBound(myArr, 2)

    Read more about UBound and LBound
    Last edited by 4x2y; Sep 2nd, 2012 at 10:13 PM.

  3. #3
    Hyperactive Member
    Join Date
    Sep 09
    Posts
    468

    Re: [RESOLVED] array lbound ubound

    Thanks, but I Not undetstand how many dimension's (myArr)

    Code:
    Private Sub Command1_Click()
    Dim myArrA(0 To 99) As String
    End Sub
    
    I want To result this "myArrA" total 1 Dimension.
    Code:
    Private Sub Command1_Click()
    Dim myArrB(0 To 99, 0 To 88, 0 To 77) As String
    End Sub
    
    I want To result this "myArrB" total 3 Dimension.
    etc...

    please!

  4. #4
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,255

    Re: array lbound ubound

    Read this article, there you will find some code to determine the number of dimensions in an array, adapt it to meet your needs.

  5. #5
    Hyperactive Member
    Join Date
    Sep 09
    Posts
    468

    Re: array lbound ubound

    thanks, I was changed. but I Not understand "Visual Basic for Applications arrays can have up to 60000"
    For 1 To 60000, Is it need? If normal around <20 dimensions, Set 99 Is it enough?

    any advice please!

    Code:
    Private Sub Command1_Click()
    Dim myArr(0 To 99, 0 To 2, 0 To 6) As String
    
    
    Debug.Print FindNumberOfDimensions(myArr), Time
    
    
    End Sub
    
    
    
    Public Function FindNumberOfDimensions(ByRef Xarray() As String, _
                                           Optional ByVal aroundDimension As Long = 9) As Long
    
    
    
    
          'Dimensions Xarray as an array.
          'Dim Xarray(1 To 10, 5 To 20, 256 To 300, 8, -5 To 0)
    
          'Sets up the error handler.
          On Error GoTo FinalDimension
    
          'Visual Basic for Applications arrays can have up to 60000
          'dimensions; this allows for that.
          For DimNum = 1 To aroundDimension
    
             'It is necessary to do something with the LBound to force it
             'to generate an error.
             ErrorCheck = LBound(Xarray, DimNum)
    
          Next DimNum
    
    
    
    'FindNumberOfDimensions = DimNum
    
    Exit Function
    
    'The error routine.
    FinalDimension:
    MsgBox "The array has " & DimNum - 1 & " dimensions"
    
    'return result:
    FindNumberOfDimensions = DimNum - 1
    
    
    End Function
    Last edited by rpool; Sep 7th, 2012 at 09:12 PM.

  6. #6
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,255

    Re: array lbound ubound

    The code in the article uses the max value of dimensions that can array has in order to ensure it will work for any given array.

    If you changed 60000 to 60 or whatever, it will not different at all, the For...Next loop will iterate until it reach the last dimension, then an error occur with this line LBound(Xarray, DimNum) and exit the loop.

  7. #7
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,255

    Re: array lbound ubound

    After looking to your adapted code, your function will return 0 always, try this one
    Code:
    Public Function FindNumberOfDimensions(ByRef Xarray() As String) As Long
        Dim DimNum As Long
        Dim ErrorCheck As Long
        
        On Error GoTo FinalDimension
        
        For DimNum = 1 To 60000
            ErrorCheck = LBound(Xarray, DimNum)
        Next DimNum
    
    FinalDimension:
        FindNumberOfDimensions = DimNum - 1
    
    End Function
    Edit:
    Oops, i didn't recognized that you have edited your post and changed the code, anyway there is no need at all for the second param aroundDimension because For...Next will never reach to 60000, it will exited once DimNum = last dimension + 1
    Last edited by 4x2y; Sep 7th, 2012 at 09:40 PM.

  8. #8
    Hyperactive Member
    Join Date
    Sep 09
    Posts
    468

    Re: array lbound ubound

    thanks, this is first known use "error_handle" to get result,
    can you tell me more any other example, used "error_handle" to get result?

  9. #9
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,255

    Re: array lbound ubound

    It is rare, but i remember that i used error handler to check if a file exist (that was before i learn more professional way to do that)
    Code:
    Public Function IsFileExist(ByVal strFileName As String) As Boolean
        Dim dblFileLen As Double
    
        On Error GoTo FileNotExist
        dblFileLen = FileLen(strFileName)
        IsFileExist = True ' no error occurs, so the file exists.
        Exit Function
    FileNotExist:
        IsFileExist = False' an error occurs, so the file not exist.
    End Function
    Also in some cases if you want to know if the program is running inside the VB IDE or it is running as EXE
    Code:
    Private Sub Command1_Click()
        On Error GoTo RunInsideIDE
        Debug.Print 0 / 0 ' This line will removed by the compiler when generating the exe.
        MsgBox "The program run as EXE"
        Exit Sub
    RunInsideIDE:
        MsgBox "The program run inside the IDE"
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •