+ Reply to Thread
Results 1 to 9 of 9

Array length

Hybrid View

mreFF Array length 07-16-2014, 01:54 PM
6StringJazzer Re: Array length 07-16-2014, 02:14 PM
mreFF Re: Array length 07-16-2014, 02:33 PM
TMS Re: Array length 07-16-2014, 03:30 PM
mreFF Re: Array length 07-16-2014, 03:54 PM
TMS Re: Array length 07-16-2014, 04:15 PM
mreFF Re: Array length 07-16-2014, 04:40 PM
TMS Re: Array length 07-16-2014, 05:00 PM
mreFF Re: Array length 07-16-2014, 05:01 PM
  1. #1
    Forum Contributor
    Join Date
    07-04-2014
    Location
    Netherlands
    MS-Off Ver
    all
    Posts
    121

    Lightbulb Array length

    The myRows() array got some value like: 1, 2, 3, 4, 5
    In my First executed code all works and gives me the right array length.
    'Sorry can't show you the data..


    ...At this time i would like to see what is left of it by Calling the updateRecords.

    Why does it execute but don't gives me the array length there?
    Are there better ways to see the Array length?


    
    
    Dim myRows()
    
    Public Sub updateRecords()
      Dim max_user_items As Long
       max_user_items = Application.CountA(myRows)
      tb_textbox1 = max_user_items & " items"
    End Sub
    
    
    
    Private Sub myBTTN_Click()
    Call updateRecords
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Array length

    The argument to CountA must be a Range object. Sometimes documentation says that it can be an array, but that is an Excel sheet array, not a VBA array variable.

    In your case, the array starts at element 0, so the number of elements is the upper bound + 1.

    UBound(myRows) + 1
    If you have bounds that are not the default, you need this:

    Dim AnotherArray(5 To 10)
    
    UBound(AnotherArray) - LBound(AnotherArray) + 1
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    07-04-2014
    Location
    Netherlands
    MS-Off Ver
    all
    Posts
    121

    Re: Array length

    I start with this this code, wich runs good:

        ReDim myRows(0)
        maxItems = theSheet.Cells(Rows.Count, 1).End(xlUp).Row
        For colomn = 1 To maxItems
        If Cells(colomn, 1) = user And Cells(colomn, 3) = "" Then
        ReDim Preserve myRows(whoIsOn)
        myRows(whoIsOn) = colomn
        whoIsOn = whoIsOn + 1
        End If
        Next colomn
    
    
       max_user_items = Application.CountA(myRows)
    Lets say MsgBox (max_user_items) got 8
    From there I walk trough the rows.
    This runs good. When adding a row the Array is still 8.
    Thats where the Call updateRecords is needed.

    How to set this up that it outputted 7 and after adding a new row 6, etc.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,493

    Re: Array length

    Not entirely sure why you need to use an array.

    Option Explicit
    
    Sub CountLoggedOn()
    
    Dim lLR As Long, lLoggedOn As Long
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    ' determine how many cells to include (using column A)
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    ' count the non-blank cells for the next column
    lLoggedOn = awf.CountA(Range("A1").Offset(0, 1).Resize(lLR))
    ' display the count
    MsgBox lLoggedOn
    
    End Sub

    This could be made into a function and, if required, called from the worksheet.
    Formula: copy to clipboard
    =fCountLoggedOn()


    Function fCountLoggedOn()
    
    Dim lLR As Long, lLoggedOn As Long
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    ' determine how many cells to include (using column A)
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    ' count the non-blank cells for the next column
    lLoggedOn = awf.CountA(Range("A1").Offset(0, 1).Resize(lLR))
    ' display the count
    fCountLoggedOn = lLoggedOn
    
    End Function
    
    Sub sTest_fCountLoggedOn()
    
    MsgBox fCountLoggedOn
    
    End Sub

    This relates specifically to column A although it could be amended to work with any range.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Contributor
    Join Date
    07-04-2014
    Location
    Netherlands
    MS-Off Ver
    all
    Posts
    121

    Re: Array length

    thnx for the inputs. What code is for output the last array item?

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,493

    Re: Array length

    Option Explicit
    
    Sub CountLoggedOn()
    
    Dim lLR As Long, lLoggedOn As Long
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    ' determine how many cells to include (using column A)
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    ' count the non-blank cells for the next column
    lLoggedOn = awf.CountA(Range("A1").Offset(0, 1).Resize(lLR))
    ' display the count
    MsgBox lLoggedOn
    
    End Sub
    
    Function fCountLoggedOn()
    
    Dim lLR As Long, lLoggedOn As Long
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    ' determine how many cells to include (using column A)
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    ' count the non-blank cells for the next column
    lLoggedOn = awf.CountA(Range("A1").Offset(0, 1).Resize(lLR))
    ' display the count
    fCountLoggedOn = lLoggedOn
    
    End Function
    
    Sub sTest_fCountLoggedOn()
    
    MsgBox fCountLoggedOn
    
    End Sub
    
    Sub sLastLoggedOn()
    
    Dim lLR As Long
    ' determine last cell (using column B)
    lLR = Range("B" & Rows.Count).End(xlUp).Row
    
    MsgBox "Row " & lLR & " :  Value: " & Range("B" & lLR).Value
    
    End Sub
    
    Function fLastLoggedOn()
    
    Dim lLR As Long
    ' determine last cell (using column B)
    lLR = Range("B" & Rows.Count).End(xlUp).Row
    
    fLastLoggedOn = "Row " & lLR & " :  Value: " & Range("B" & lLR).Value
    
    End Function
    
    Sub sTest_fLastLoggedOn()
    
    MsgBox fLastLoggedOn
    
    End Sub

    Regards, TMS

  7. #7
    Forum Contributor
    Join Date
    07-04-2014
    Location
    Netherlands
    MS-Off Ver
    all
    Posts
    121

    Re: Array length

    Sorry, I'm strugling with my own code first.

    At this moment its hard to find the latest array.
    I think I started with two different wrong numbers. Zero and One.
    I can't find where this bug is. Everything is working fine, except the latest handling.

    
            Dim max As Long
    
            max = Application.CountA(myRows)
            
    
    
     If whoIsOn < max - 1 Then
                    
                    whoIsOn = whoIsOn + 1
                    Call Forward
           
            End If
    Above do the trick for everything but.... this:

    Ive found out that this code below will allways be one number short.
    It never match to the exact value.


    
      If whoIsOn = max - 1  Then
           
           MsgBox "latest record"
           
           End If
    Changing to this won't help neither.
    whoIsOn +1 = max -1
    whoIsOn = max
    whoIsOn = max -1

    If I do this all they futher actions are mixed up! ^&@*##@^$

    What goes wrong!>&#$(@

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,493

    Re: Array length

    OK, I'm struggling too. I have no idea what your data looks like, what the end to end code looks like, or what you are trying to achieve.

    When you are given code, you know all this ... but we don't. So it's hard to work out what it needs to do.


    Regards, TMS

  9. #9
    Forum Contributor
    Join Date
    07-04-2014
    Location
    Netherlands
    MS-Off Ver
    all
    Posts
    121

    Re: Array length

    OUTPUT:

    Item whoIsOn: 0 - MAX ITEMS 6
    Item whoIsOn: 1 - MAX ITEMS 6
    Item whoIsOn: 2 - MAX ITEMS 6
    Item whoIsOn: 3 - MAX ITEMS 6
    Item whoIsOn: 4 - MAX ITEMS 6 = Latest record ???????????
    Item whoIsOn: 5 - MAX ITEMS 6 = Latest record ???????????

    Ok Item whoIsOn: 4 is exctually whoIsOn 5 becuz it starts @ zero. And than max -1 = one less if there are 6 items in this case.
    So 5 is indeed the latest record
    Whats the why to solve this? To get a proper value "Latest record" in only Item whoIsOn: 5 - MAX ITEMS 6

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Limiting the array length in LOOKUP
    By Peterb1 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-03-2014, 04:03 PM
  2. [SOLVED] VBA to change Array Length!
    By sianjialin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-16-2013, 05:27 AM
  3. Max Length in Array
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-21-2010, 05:42 AM
  4. Length of Array question
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-24-2008, 12:24 AM
  5. [SOLVED] length of an array
    By april27 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-19-2006, 10:10 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1