+ Reply to Thread
Results 1 to 12 of 12

Length of Array question

Hybrid View

welchs101 Length of Array question 12-23-2008, 09:00 AM
dominicb Good afternoon welchs101 ... 12-23-2008, 09:13 AM
welchs101 I had not thought of doing... 12-23-2008, 09:22 AM
welchs101 getting an error 12-23-2008, 06:26 PM
shg Array must be dynamic when... 12-23-2008, 06:38 PM
  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Length of Array question

    I have a STRING array which is dimensioned with 100 elements possible. What i want to know is how to determine the "length" (not sure if this is the right word) of the array. For example: Suppose i have 10 strings in the array. Is there a command to determine that there are only 10 elements in the 100 possilble array?

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon welchs101
    Quote Originally Posted by welchs101 View Post
    I have a STRING array which is dimensioned with 100 elements possible. What i want to know is how to determine the "length" (not sure if this is the right word) of the array.
    You would have to loop through the array and test each string, and count at which point an empty string is returned.

    FWIW I think you're using arrays incorrectly. The idea is that if you don't know how big your array's going to be you set it up using empty parnetheses. Then everytime you add an element to the array you use Redim Preserve to re-dimension the array and keep the previous contents of the array unchanged :
    Sub ArrayTest()
    Dim test()
    For n = 1 To 10
    ReDim Preserve test(n)
    test(n) = n
    Next n
    MsgBox UBound(test)
    End Sub
    The last UBound statement will return how many elements are in your array - you can change the value of 10 to anything - the array will always expand to accept the next value.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    I had not thought of doing arrays in this fashion. Thanks.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    getting an error

    i tried what was suggested but i am getting an error.......subset of code is shown below:

    Sub get_HW_Quiz_cols(Gradebook_HW_col() As String, Inputfile_HW_col() As String, Gradebook_quiz_col() As String, Inputfile_quiz_col() As String, _
    Gradebook_Filename As String, HW_InputFilename As String, HW_InputFile_Sheet As String)
    
    Dim temprange As Range
    Dim temp As Integer
    
    'get HW and Quiz cols from Gradebook
    Set temprange = Workbooks(Gradebook_Filename).Worksheets(Gradebook_Sht_Nm_HW).Range("1:1")
    
    For z = 1 To Max_HW_grades
    temp = Find_next_occurance(temprange, "HW" & z, "col")
    If temp <> 0 Then
    ReDim Preserve Gradebook_HW_col(z)
    Gradebook_HW_col(z) = alphacol(temp)
    End If
    Next z
    
    'get HW and Quiz cols from Gradebook
    Set temprange = Workbooks(HW_InputFilename).Worksheets(HW_InputFile_Sheet).Range("1:1")
    
    For z = 1 To Max_HW_grades
    temp = Find_next_occurance(temprange, "HW" & z, "col")
    If temp <> 0 Then
    ReDim Preserve Inputfile_HW_col(z)
    Inputfile_HW_col(z) = alphacol(temp)
    End If
    Next z
    
    End Sub



    error occurs on the ReDim statements..........

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Array must be dynamic when originally declared to be resized, e.g.,
    Sub x()
        Dim a() As String
        ReDim a(1 To 20)
        
        y a
        MsgBox UBound(a)
    End Sub
    
    Sub y(a() As String)
        ReDim Preserve a(1 To 10)
    End Sub
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712
    The array is declared as dynamic.

    Do you need the initial ReDim 1 to 20 statement?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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