Consulting

Results 1 to 2 of 2

Thread: Array issues - one vs two dimensional

  1. #1
    VBAX Contributor
    Joined
    Jul 2017
    Location
    Zurich
    Posts
    132
    Location

    Post Array issues - one vs two dimensional

    Hi Folks

    I've always had trouble with arrays; should they start from 0 or 1? mulitdimesion or not?....
    Currently, I'm trying to declare two one dimesional arrays and then consolidate them in another array.

    Simple example (with Option base 1):

    arrCardsCategories = Array("Hearts", "Tiles", "Pikes", "Clovers")arrCardsValues = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13")
    ReDim arrCardsTotal(1 To UBound(arrCardsCategories) * UBound(arrCardsValues), 1 To 1)

    for i = 1 to ubound(arrCardsCategories)
    for j = 1 to ubound(arrCardsValues)
    arrCardsTotal(i, j) = arrCardsCategories(i) & arrCardsValues(j)
    next j
    next i

    The problem is that the first two arrays are "one-dimensional", while the other array is 2-dimensional.

    In essence, I just want to put those ****ing 52 cards in one array (1 to 52). Is there a way to define simple arrays like above to two-dimensional?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    With Option Base 1, arrCardsCategories goes from 1 - 4

    Without Option Base 1 (the default), arrCardsCategories goes from 0 - 3

    1. I always like to use LBound and UBound

    2. You can start the array bounds anywhere -- e.g. I've used Dim Ary(23 to 48) as String when I needed to process rows 23 through 48, just kept things neater (IMVHO)


    Not sure why you need a 2-dim array anywhere


    Option Explicit
    
    
    Sub phh()
        Dim arrCardsCategories As Variant, arrCardsValues As Variant, arrCardsTotal() As String
        Dim i As Long, j As Long, k As Long
    
    
        arrCardsCategories = Array("Hearts", "Tiles", "Pikes", "Clovers")       '   0 - 3
        arrCardsValues = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13") '   0 - 12
    
    
        ReDim arrCardsTotal(0 To (UBound(arrCardsCategories) + 1) * (UBound(arrCardsValues) + 1) - 1)   ' 0 - 51
    
    
        k = 0
    
    
        For i = LBound(arrCardsCategories) To UBound(arrCardsCategories)
            For j = LBound(arrCardsValues) To UBound(arrCardsValues)
                arrCardsTotal(k) = arrCardsCategories(i) & arrCardsValues(j)
                k = k + 1
            Next j
        Next i
    End Sub

    Edit -- added a link that you might find useful

    https://bytecomb.com/arrays-in-vba-p...ignment-rules/
    Last edited by Paul_Hossler; 12-06-2019 at 07:47 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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