Results 1 to 4 of 4

Thread: error collecting, sorting data in Arrays in Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    13

    error collecting, sorting data in Arrays in Excel

    Hi there,
    I'm a newbie with some prog exp but I inherited this code and can't get it to run -- seems like something with the contents of the arrays, or maybe i'm missing code to "close" the arrays, if such a thing is needed - -everything runs fine until the second to last line, when I try and create a new worksheet where I can dump all the data I've accumulated in arrays, in order to sort and filter it. (Gives me runtime error 9 - subscript out of range)
    The way this prog works - it has a wksheet with reports listed along with checkboxes - If a report is checked, prog will go to another column on that row and pick up the necessary columns it will need to look up in a datadump worksheet. It will go to that worksheet and place all records with "TRV"
    in a column and pick up 3 pieces of data in other colums and save the results to TRVsecurities(TRVcount) = myRow.
    Sorry for the long email -- any help would be greatly appreciated.
    BTW any help with getting rid of these Active.cell statements or better/easier design would greatly be appreciated.
    Fred

    VB Code:
    1. Sub runReport8(ByVal rowIndx As Integer)
    2.  
    3.     Workbooks("aegis_reporting.xls").Activate
    4.     Worksheets("Reports").Activate
    5.     Range("A" & rowIndx).Select
    6.    
    7.     Dim rowControl2 As Integer
    8.     Dim leftOffOn As Integer
    9.    
    10.     Dim myReportName As String
    11.     myReportName = ActiveCell.Value
    12.    
    13.     Dim mySheetName As String
    14.     Range("G" & rowIndx).Select
    15.     mySheetName = ActiveCell.Value
    16.    
    17.     Dim columnParameters As String
    18.     Dim colParamArray() As String
    19.     Range("H" & rowIndx).Select
    20.     columnParameters = ActiveCell.Value
    21.     colParamArray = Split(columnParameters, ",")
    22.     'B,C,J,P,Q
    23.     '0,1,2,3,4
    24.    
    25.     MsgBox columnParameters
    26.    
    27.     Worksheets(mySheetName).Activate
    28.     Range("B2").Select
    29.    
    30.     Dim lastRow As Integer
    31.    
    32.     ActiveCell.End(xlDown).Select
    33.     lastRow = ActiveCell.Row
    34.    
    35.     Range(colParamArray(0) & "2").Select
    36.    
    37.     Dim rowControl, mysteryRow As Integer
    38.     Dim USCPAcount, MMFcount, TRVcount As Integer
    39.    
    40.     Dim TRVsecurities(1000) As String
    41.     Dim myRow As String
    42.    
    43.     For rowControl = 2 To lastRow
    44.         mysteryRow = ActiveCell.Row
    45.        
    46.         If ActiveCell.Text = "TRV" Then
    47.            ' Range(colParamArray(2) & ActiveCell.Row).Value - _
    48.            ' Range(colParamArray(3) & ActiveCell.Row).Value
    49.            
    50.             myRow = ActiveCell.Value
    51.             Debug.Print myRow
    52.             TRVcount = TRVcount + 1
    53.             Range(colParamArray(1) & mysteryRow).Select
    54.             myRow = ActiveCell.Value
    55.             Debug.Print myRow
    56.             Range(colParamArray(2) & mysteryRow).Select
    57.             myRow = myRow & ":" & ActiveCell.Value
    58.             Debug.Print myRow
    59.             Range(colParamArray(3) & mysteryRow).Select
    60.             myRow = myRow & ":" & ActiveCell.Value
    61.             Debug.Print myRow
    62.             TRVsecurities(TRVcount) = myRow
    63.             Debug.Print myRow
    64.             Range(colParamArray(0) & mysteryRow).Select
    65.         End If
    66.    
    67.         ActiveCell.Offset(1, 0).Select
    68.     Next rowControl
    69.    
    70.     'START TRV block
    71.    
    72.     Workbooks("aegis_reporting.xls").Activate
    73. [COLOR=DarkGreen]    Worksheets(myReportName & "_Sort").Activate[/COLOR]
    74.     ' Gives me runtime error 9 - subscript out of range
    75.     Call clearSheet
    Last edited by si_the_geek; May 24th, 2006 at 08:01 PM. Reason: added vbcode tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: error collecting, sorting data in Arrays in Excel

    Welcome to VBForums!

    The line with the error is a problem because you arent adding a new worksheet - you are simply trying to activate it. To add a new one (which should be added just before that line), you can do this:
    VB Code:
    1. With Workbooks("aegis_reporting.xls")
    2.       .WorkSheets.Add
    3.       .WorkSheets(.WorkSheets.Count).Name = myReportName & "_Sort"
    4.     End With

    As to removing Activecell, here's an example. You can change this:
    VB Code:
    1. Range(colParamArray(1) & mysteryRow).Select
    2.             myRow = ActiveCell.Value
    To this:
    VB Code:
    1. myRow = Range(colParamArray(1) & mysteryRow).Value
    Note that as you are joining strings, this would be (slightly) more efficient like this:
    VB Code:
    1. myRow = Cells(mysteryRow, colParamArray(1)).Value


    Rather than using mysteryRow (and the ActiveCell) to get the row, why arent you using your loop variable (rowControl) instead?

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    13

    Adding/Deleting Worksheets in Excel;

    Si,
    Thanks so much for the help. It creates worksheet just fine, though now It's also creating several other sheets: Sheet1, Sheet2, Sheet3 in addition to myReportName & "_Sort". (they are blank)

    Also, when I tried to delete it at a later time with:

    VB Code:
    1. With Workbooks("aegis_reporting-TEST.xls")
    2.           .Worksheets(myReportName & "_Sort").Delete
    3.     End With

    it prompts users with a msg box on wether they want to delete a sheet that might have data in it. Any suggestions to disable the msgbox.

    Thanks again for your -- I'm actually thinking now with the amount of reports that I have to create I might restart this project in Access -- I will ask opinions on design in another post.
    Thx

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: error collecting, sorting data in Arrays in Excel

    You can disable alerts before you delete the sheet, just make sure you re-enable them after the delete.
    VB Code:
    1. Application.DisplayAlerts = False
    2. With Workbooks("aegis_reporting-TEST.xls")
    3.     .Worksheets(myReportName & "_Sort").Delete
    4. End With
    5. Application.DisplayAlerts = True
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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