Results 1 to 4 of 4

Thread: subscript out of range (Excel)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    2

    subscript out of range (Excel)

    long story short, i haven't used dynamic variables before and i'm not sure why i keep getting this subscript out of range error -- as soon as it gets to the row i've starred it crashes wih the error - in the debugger i can see that the value of ActiveCell is just "Trade Ticket"

    VB Code:
    1. Dim trigger As String
    2.     Dim Labels() As String
    3.     Dim Data() As Variant
    4.     Dim inputRow As Integer
    5.     Dim inputCol As Integer
    6.     Dim counter As Integer
    7.  
    8.     counter = 0
    9.     inputRow = 1
    10.     inputCol = 1
    11.     Do While trigger <> "InputEnd" ' finds rows with data
    12.         If trigger = "Input" Then ' takes data out of relevant rows
    13.             inputRow = ActiveCell.Row
    14.             inputCol = ActiveCell.Column
    15.             Do While ActiveCell.Offset(0, 1) <> "End"
    16.                 Label = ActiveCell.Value
    17.         ****        Labels(counter) = ActiveCell.Value
    18.                 Data(counter) = ActiveCell.Offset(1, 0).Value
    19.                 counter = counter + 1
    20.             Loop
    21.         Else
    22.         End If
    23.         inputRow = inputRow + 1
    24.         Cells(inputRow, inputCol).Select
    25.         trigger = ActiveCell.Value
    26.     Loop
    Last edited by RobDog888; Jul 7th, 2006 at 01:52 PM. Reason: Added [vbcode] tags

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: subscript out of range (Excel)

    Welcome to the Forums.

    I dont see where your dimensioning the array Data or Labels with the number of elements needed.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: subscript out of range (Excel)

    you need to Set then increase the amount of "space" that each variable has

    Normally an array has as many spaces as its set to..

    DIm tmp(3) as string

    tmp has 4 spaces for data 0,1,2,3

    now a dynamic array is not "setup"
    DIm tmp() as string
    theres no spaces.. you need to Redim to start it then increase it

    Redim tmp(0)
    then
    Redim Preserve tmp(1) <-using preserve keeps any data in the array

    VB Code:
    1. Dim trigger As String
    2.     Dim Labels() As String
    3.     Dim Data() As Variant
    4.     Dim inputRow As Integer
    5.     Dim inputCol As Integer
    6.     Dim counter As Integer
    7.    
    8.     counter = 0
    9.     inputRow = 1
    10.     inputCol = 1
    11.    
    12.    
    13.    
    14.     Do While trigger <> "InputEnd"
    15.         If trigger = "Input" Then
    16.             inputRow = ActiveCell.Row
    17.             inputCol = ActiveCell.Column
    18.             Do While ActiveCell.Offset(0, 1) <> "End"
    19.                 Label = ActiveCell.Value
    20.                 [B]If counter = 0 Then
    21.                     ReDim Labels(0)
    22.                     ReDim Data(0)
    23.                 Else
    24.                     ReDim Preserve Labels(counter)
    25.                     ReDim Preserve Data(counter)
    26.                 End If[/B]
    27.                 Labels(counter) = ActiveCell.Value
    28.                 Data(counter) = ActiveCell.Offset(1, 0).Value
    29.                 counter = counter + 1
    30.             Loop
    31.         Else
    32.         End If
    33.         inputRow = inputRow + 1
    34.         Cells(inputRow, inputCol).Select
    35.         trigger = ActiveCell.Value
    36.     Loop
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    2

    Re: subscript out of range (Excel)

    ok - i get it, i must have gotten confused when i was reading through examples, i thought Labels() would just set up a dynamic array -- for this problem it's fine to just set it up as Labels(100) and eveything seems to be working perfectly, i'll play around with the new info you gave me and see if i can get it to work both ways

    thanks for the help

Posting Permissions

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



Click Here to Expand Forum to Full Width