none
Issues declaring and preserving array variables RRS feed

  • Question

  • I am having lots of trouble with variable arrays.

    I know that when declaring arrays, they are declared as variants.

    Here is the code snippet below:

    Sub FindSets()
    Dim ConceptNum As Integer
    Dim NumCpts, NumCptsLimit As Long
    Dim strX As String
    
    Open "C:\Users\John\Documents\Page 14.txt" For Input As #1
    Open "C:\Users\John\Documents\Nouns.txt" For Output As #2
    
    
    NumCptsLimit = InputBox("How many concept entries?", "Number of concept entries", 1)
    Input #1, strX
        For NumCpts = 1 To NumCptsLimit
            ConceptNum = InputBox("Which concept entry?", "Input Concept Entry")
            Print #2, strX; "/"; ConceptNum
        Next
        
        
    Close
    
    End Sub
    

    Although it runs well, it needs some more work.  This time, I want to store that variable within an array.

    Instead of declaring them individually.

    For example:

    ConcptNum(1)=Inputbox()

    ConcptNum(2)=Inputbox()

    ...and so forth.

    Would be this instead:

    for x=1 to N

    ConcptNum(x)=inputbox()

    next

    The only problem, which lies beyond, is that I keep getting subscript array error messages.

    Anyone who knows about array input and output would be beneficial in helping me.

    Regards,

    JohnDBCTX


    jp

    Saturday, December 24, 2016 4:57 AM

Answers

  • Okay, Jan and other users.

    For students, and student-to-be users, I am being open about this topic.

    This time, check out my updated code snippet, see what you think.

    Sub FindSets()
    Dim ConcptNum() As Integer
    Dim NumberOfConcepts As Integer
    Dim Words, NumOfWords As Integer
    Dim WordElement As String
    
    Open "C:\Users\John\Documents\Output File 1.txt" For Output As #2
    Open "C:\Users\John\Documents\Output File 2.txt" For Output As #3
    
    NumberOfConcepts = InputBox("How many concepts?", "Input number of concepts", 1)
    NumOfWords = InputBox("How many words?", "Input number of words", 1)
    
    ReDim ConcptNum(NumberOfConcepts)
        For I = 0 To UBound(ConcptNum)
            ConcptNum(I) = InputBox("Enter a number", "Input number", 0)
            Print #2, ConcptNum(I)
            For Words = 0 To NumOfWords
                WordElement = InputBox("Enter a word", "Input word")
                Print #3, WordElement, ConcptNum(I)
            Next
        Next
    
    Close
    
    End Sub
    

    Keep this in mind to other users that this would be helpful.

    I do hope I get my point across.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Sunday, December 25, 2016 7:00 AM
    Sunday, December 25, 2016 6:59 AM

All replies

  • John,

    To fill the array you have to set its dimension with ReDim.
    Perhaps this will do:

    Sub FindSets()
    Dim ConcptNum() As Variant
    Dim NumCpts As Long
    Dim NumCptsLimit As Variant
    
    NumCptsLimit = InputBox("How many concept entries?", "Number of concept entries", 1)
    If IsNumeric(NumCptsLimit) Then
        ReDim ConcptNum(NumCptsLimit)
        For NumCpts = 0 To NumCptsLimit - 1
            ConcptNum(NumCpts) = InputBox("Which concept entry?", "Input Concept Entry")
        Next
    End If
    End Sub
    

    Jan

    Saturday, December 24, 2016 8:40 AM
  • If you know the data type you can declare the array with that type

    Dim i() as Integer

    Dim s() as Single

    It saves space and it slightly faster to use the right type.

    You need to Redim the array before use

    Redim i(100)

    If you don't know the size you can Redim the array again to increase the size.  You need to preserve the existing values.  You can use UBound to find last element in array.

    Redim i(0)  ' Array has 1 element

    i(UBound(i)) = 100  ' Assign 100 to last element, index 0

    ReDim Preserve i(UBound(i) + 1)  'Add 1 element to end of array.  Preserve 100 at index 0.

    i(UBound(i)) = 101  ' Assign 101 to last element, index 1

    Saturday, December 24, 2016 12:36 PM
  • Can anyone provide me some examples of UBound?

    Which steps do I need to take before using UBound correctly?

    Regards,

    JohnDBCTX


    jp

    Saturday, December 24, 2016 1:33 PM
  • John,

    Before using UBound you have to give the size to your array either with the declaration:

    Dim ConcptNum(7) as Integer
    'from now on you can use UBound

    or with ReDim after a declaration without the size:

    Dim ConcptNum() as Integer
    'other code
    ReDim ConcptNum(7)
    'from now on you can use UBound

    Jan

    Saturday, December 24, 2016 3:42 PM
  • Okay, Jan and other users.

    For students, and student-to-be users, I am being open about this topic.

    This time, check out my updated code snippet, see what you think.

    Sub FindSets()
    Dim ConcptNum() As Integer
    Dim NumberOfConcepts As Integer
    Dim Words, NumOfWords As Integer
    Dim WordElement As String
    
    Open "C:\Users\John\Documents\Output File 1.txt" For Output As #2
    Open "C:\Users\John\Documents\Output File 2.txt" For Output As #3
    
    NumberOfConcepts = InputBox("How many concepts?", "Input number of concepts", 1)
    NumOfWords = InputBox("How many words?", "Input number of words", 1)
    
    ReDim ConcptNum(NumberOfConcepts)
        For I = 0 To UBound(ConcptNum)
            ConcptNum(I) = InputBox("Enter a number", "Input number", 0)
            Print #2, ConcptNum(I)
            For Words = 0 To NumOfWords
                WordElement = InputBox("Enter a word", "Input word")
                Print #3, WordElement, ConcptNum(I)
            Next
        Next
    
    Close
    
    End Sub
    

    Keep this in mind to other users that this would be helpful.

    I do hope I get my point across.

    Regards,

    JohnDBCTX


    jp

    • Marked as answer by JohnDBCTX Sunday, December 25, 2016 7:00 AM
    Sunday, December 25, 2016 6:59 AM