+ Reply to Thread
Results 1 to 4 of 4

have 1 dimensional array, trying to create a 2 dimensional array, runtime 9

Hybrid View

dmcgov have 1 dimensional array,... 03-21-2019, 06:40 AM
dmcgov Re: have 1 dimensional array,... 03-21-2019, 06:50 AM
6StringJazzer Re: have 1 dimensional array,... 03-21-2019, 06:56 AM
dmcgov Re: have 1 dimensional array,... 03-21-2019, 07:03 AM
  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    have 1 dimensional array, trying to create a 2 dimensional array, runtime 9

    so i have the test code below but im getting a run-time error '9': subscript out of range. it errors on this line: Debug.Print z(w, y)
    when i check the locals window, the 2 dimensional array says z(1,1) and z(1,2) but w is 0 and y is 1. what am i doing wrong?

    Sub test_uf()
        Dim w
        Dim s
        Dim r
        Dim z() As String
        Dim t
        
        count = 2
        For y = 1 To count
            btn_Gen_uf2.TextBox1.SetFocus
            btn_Gen_uf2.Show
            t = Split(btn_Gen_uf2.TextBox1.Text, vbCrLf)
            Debug.Print "lbound(t) is " & LBound(t)
            
            ReDim z(UBound(t), count)
            For w = LBound(t) To UBound(t)
                MsgBox t(w)
                Debug.Print "z(" & w & "," & y & ") = " & t(w)
                Debug.Print z(w, y)
            Next w
            'Exit Sub
        Next y
    End Sub

  2. #2
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: have 1 dimensional array, trying to create a 2 dimensional array, runtime 9

    never mind, i fixed the issue. missed that i had option base 1 at the top of the module.

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

    Re: have 1 dimensional array, trying to create a 2 dimensional array, runtime 9

    It is not clear what you are trying to do, and there may be a better overall solution. For starters, you never assign values to any element of z, yet you are printing it out. So you are always going to print 0.

    Note that LBound(t) will be 0, due to what Split returns. So the first value of w in your loop will be 0. If you are using "Option Base 1" at the top of the module, then the first dimension of z will be 1 To UBound(t) and you will get a subscript error.

    I recommend you make your lower bounds explicit and try this:

    ReDim z(0 To UBound(t), 1 To count)
    but I can't say with 100% certainty because I don't know what this code is supposed to do.

    (Also I highly recommend using variable names that are meaningful, rather than single letters.)
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: have 1 dimensional array, trying to create a 2 dimensional array, runtime 9

    so ya, i did have option base 1 for my test code, but i removed that and the test procedure works great. but when i implement it in my actual code,it fails on: z(w,y)=t(w)

    here is the real code.

    Sub showuserform1()
    
    Dim t() As String
    Dim x, y
    Dim z() As String
    
    Set btn_nm = New Scripting.Dictionary
    Set lbl_tx = New Scripting.Dictionary
    Set code = New Scripting.Dictionary
    
    repeatx:
    x = UCase(InputBox("(H)orizontal or (V)ertical?", "Orientation", "H"))
    If x = "H" Then
        orientation = "Horizontal"
    ElseIf x = "V" Then
        orientation = "Vertical"
    Else
        MsgBox ("Input either 'H' or 'V'")
        GoTo repeatx
    End If
    repeatcount:
    count = InputBox("How many buttons do you want? ", "Button Count", "1")
    If count < 1 Then
        MsgBox ("Input Quantity of Buttons, enter at least 1")
        GoTo repeatcount:
    End If
    
    For y = 1 To count
        btn_nm(y) = InputBox("What do you want CommandButton" & y + 1 & " to say?", "CommandButton name", "CommandButton" & y)
        lbl_tx(y) = InputBox("What do you want Label" & y + 1 & " to say?", "Label text", "Label" & y)
        btn_Gen_uf2.Label1.Caption = "Enter Code in Window - max 8k characters"
        btn_Gen_uf2.TextBox1.Value = ""
    reshow:
        btn_Gen_uf2.TextBox1.SetFocus
        btn_Gen_uf2.Show
        If btn_Gen_uf2.TextBox1.Value = "" Then
            MsgBox "Enter Code in Window before selecting OK"
            GoTo reshow:
        End If
        t = Split(btn_Gen_uf2.TextBox1.Text, vbCrLf)
        ReDim z(LBound(t), count)
        For w = LBound(t) To UBound(t)
            MsgBox t(w)
            Debug.Print "z(" & w & "," & y & ") = " & t(w)
            z(w, y) = t(w)
        Next w
        Exit Sub
        For s = 1 To count
            For r = 0 To UBound(t)
                Debug.Print z(r, s)
            Next r
        Next s
    Next
    so what should happen is that when the code window opens up (btn_Gen_uf2.show), it parses the text in the textbox and should populate each line into z(w,y). but when i check the local window this time, it shows z(0,0),z(0,1) & z(0,2) and only z(0,1) shows one of the text lines, but the other two are "". i know that sometimes single letters can be confusing so i will try to implement that in the future.

    nevermind, your redim code worked for me. sorry that i glossed over it. thanks
    Last edited by dmcgov; 03-21-2019 at 07:06 AM. Reason: working now

+ 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. [SOLVED] Convert 2 dimensional array to 1 dimensional
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2018, 05:20 AM
  2. Help converting one-dimensional array to multi-dimensional array
    By puzzlelover22 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2016, 06:48 AM
  3. Parse Data from one dimensional array into a 2 dimensional array.
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-30-2016, 07:29 AM
  4. [SOLVED] Convert one dimensional array into two dimensional array
    By mohammed sabr in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-26-2015, 10:34 AM
  5. Replies: 6
    Last Post: 09-25-2013, 10:08 PM
  6. Creating a 2-dimensional array from a 1-dimensional list
    By guywithcamera in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-27-2008, 06:34 PM
  7. Create One-Dimensional Array from Two-Dimensional Array
    By Stratuser in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2005, 05:06 PM

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