Consulting

Results 1 to 6 of 6

Thread: Issue with getting last member from an array

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    22
    Location

    Issue with getting last member from an array

    Hi, I cannot get the last item from my selection. Coding in VBA drives my crazy. There might by severar errors in the following short code. Thank you for your help.

    Option Base 1
    Sub test()
    Dim Ranko As Integer
    LastItem As Date ' not sure if this is a correct declaration
    
    Sheets("SCHEDULE").Range("E5:E12").Select' my selection of some dates
    
    
    Ranko = Selection.Count ' counts the cells in selection
    Dim AllTimeArr(Ranko) As Variant
    AllTimeArr = Selection 'should assign the selection to the array
    LastItem = AllTimeArr(Ranko) 'should get the last member from array
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Selection is a 2 dim array

    Option Explicit
    Option Base 1
    Sub test()
        Dim Ranko As Integer
        Dim LastItem As Date ' not sure if this is a correct declaration
        Dim AllTimeArr As Variant
         
        Sheets("SCHEDULE").Range("E5:E12").Select ' my selection of some dates
        AllTimeArr = Selection '2 dim array (1-8,1-1)
        
        Ranko = UBound(AllTimeArr, 1)
        
        LastItem = AllTimeArr(Ranko, 1) 'last row, first col
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    22
    Location
    I did not expect that. Thank you!
    And how would you extract only the one dimmension? Because this doesn't work:

    Ranko = UBound(AllTimeArr, 1) 
    Dim ExtrArray(Ranko) As Date
    For i = 1 To Ranko
    ExtrArray(i) = AllTimeArr(i, 1)
    Next

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Keep it ....

    Sub M__snb()
      sn=Sheets("SCHEDULE").Range("E5:E12")
      msgbox sn(ubound(sn),1)
    end sub
    1-dimensional:
    Sub M__snb()
      sn=application.transpose(Sheets("SCHEDULE").Range("E5:E12"))
      msgbox sn(ubound(sn))
    end sub
    or

    Sub M__snb()
      sn=[transpose(E5:E12)]
      msgbox sn(ubound(sn))
    end sub

  5. #5
    VBAX Regular
    Joined
    May 2016
    Posts
    22
    Location
    ...found solution for that one:
    Dim ExtrArray() As Variant
        Ranko = UBound(AllTimeArr, 1)
        ReDim ExtrArray(Ranko)
        For i = 1 To Ranko
        ExtrArray(i) = AllTimeArr(i, 1)
        Next

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    snb's #4 for a 1 dim array using .Transpose is better and faster
    ---------------------------------------------------------------------------------------------------------------------

    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

Tags for this Thread

Posting Permissions

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