+ Reply to Thread
Results 1 to 21 of 21

run time error 9 : subscript out of range

Hybrid View

  1. #1
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    run time error 9 : subscript out of range

    This program is supposed to take the value in two combo boxes and use them to populate pivot charts that are in other spreadsheets. So a user would select PS and AMI on sheet one (s) and it would change the pivot table on s1-s9 to look up those chosen fields. I keep getting a subscript out of range error, and I'm not sure if my pivottable.pivotlayout method is correct, but when I recorded a macro (in the very bottom) using activesheet, it worked. How can I fix this?

    The error occurs at the first large text, the second large text is the recorded macro.

    Private Sub ChooseButton_Click()
    
    Dim s As String
    Dim ws As Worksheet
    Dim s1 As String
    Dim ws1 As Worksheet
    Dim s2 As String
    Dim ws2 As Worksheet
    Dim s3 As String
    Dim ws3 As Worksheet
    Dim s4 As String
    Dim ws4 As Worksheet
    Dim s5 As String
    Dim ws5 As Worksheet
    Dim s6 As String
    Dim ws6 As Worksheet
    Dim s7 As String
    Dim ws7 As Worksheet
    Dim s8 As String
    Dim ws8 As Worksheet
    Dim s9 As String
    Dim ws9 As Worksheet
    
    
    s = ".csv]Oppty Created to R1 Date L"
    s1 = "Opportunity Created"
    s2 = "Submit to ComOps"
    s3 = "Bid Due"
    s4 = "Expected Order"
    s5 = "R1"
    s6 = "Proposal Promise"
    s7 = "R2"
    s8 = "Proposal Submit"
    s9 = "R3"
    
    Set ws = Workbooks("Leading Indicators FW33.xls").Worksheets(s)
    Set ws1 = Workbooks("Leading Indicators FW33.xls").Worksheets(s1)
    'Set ws2 = Workbooks("Leading Indicators FW33.xls").Worksheets(s2)
    'Set ws3 = Workbooks("Leading Indicators FW33.xls").Worksheets(s3)
    'Set ws4 = Workbooks("Leading Indicators FW33.xls").Worksheets(s4)
    'Set ws5 = Workbooks("Leading Indicators FW33.xls").Worksheets(s5)
    'Set ws6 = Workbooks("Leading Indicators FW33.xls").Worksheets(s6)
    'Set ws7 = Workbooks("Leading Indicators FW33.xls").Worksheets(s7)
    'Set ws8 = Workbooks("Leading Indicators FW33.xls").Worksheets(s8)
    'Set ws9 = Workbooks("Leading Indicators FW33.xls").Worksheets(s9)
    
     ws1.PivotTables("pivottable10").PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2
     ws1.PivotTables("pivottable10").PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1
     
     'ws2.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws2.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
    ws3.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws3.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
    ws4.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws4.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
    ws5.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws5.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
    ws6.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws6.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
    ws7.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws7.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
    ws8.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws8.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1 _
        ws9.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 _
     ws9.PivotLayout.PivotFields("Sub P&L").CurrentPage = ComboBox1
        
        'ActiveChart.PivotLayout.PivotFields("Sub P&L").CurrentPage = "(All)" _
        Sheets("Sumbit to ComOps").Select _
        ActiveChart.PivotLayout.PivotFields("P&L").CurrentPage = "PS" _
        ActiveChart.PivotLayout.PivotFields("Sub P&L").CurrentPage = "AMI" _
        ActiveChart.PivotLayout.PivotFields("Sub P&L").CurrentPage = "(All)" _
        Sheets("Bid Due").Select
    'End Sub
    
    End Sub

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: run time error 9 : subscript out of range

    That would imply that either the workbook name or worksheet name are incorrect.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    I have checked that, its not the case. I copied the names of the tabs directly from the sheet tabs, and then also copied the workbook name directly. No fix.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: run time error 9 : subscript out of range

    Or the workbook is not open.

    That about rounds out the possibilities.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    i'm sure this is about to make me look like an idiot

    all the sheets are in the same file, do I need to reference the workbook?

    I'm an idiot, right?

    I should also say this is only the third code I have made.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: run time error 9 : subscript out of range

    You need to if it's not the activeworkbook, but it's better practice to provide the complete reference in any case.

  7. #7
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    does that have a different name for the code?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: run time error 9 : subscript out of range

    Yes:
        Dim cht As Chart
    
        Set cht = Sheets("Chart1")
        MsgBox cht.Name

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: run time error 9 : subscript out of range

    Note that you also cannot then refer to a PivotTable on that sheet, since there isn't one. You need to refer to the sheet that contains the table the chart is linked to.

  10. #10
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    ws As worksheet 
    ws1 As worksheet
    s as string
    s1 as string
    cht as chart
    
    s = ".csv Oppty Created to R1 Date L"
    s1 = "Opportunity Created"
    set cht = sheets(s1)
    so on the next lines, should I change ws1 to .charts(cht)?
    should it be cht.pivottables ... ? I'm getting an object doesnt support property or method on the pivot tables line.

    Set ws = Workbooks("Leading Indicators FW33.xls").Worksheets(s)
    Set ws1 = Workbooks("Leading Indicators FW33.xls").charts(cht)
    ' this above line gets a type mismatch like this
    'if i change this next line to cht.pivottables I get the object doesn't support property 
    
     ws1.PivotTables("PivotTable10").PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2
     ws1.PivotTables("PivotTable10").PivotLayout.PivotFields("P&L").CurrentPage = ComboBox1

    This code is causing me a lot of grief.

  11. #11
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    Unable to set the _Default property of the PivotItem class
    Private Sub ChooseButton_Click()
    
    Dim s As String
    Dim s1 As String
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim cht As Chart
    
    s = ".csv Oppty Created to R1 Date L"
    s1 = "Opportunity Created" 'this is a pivot chart
    
    Set cht = Charts(s1)
    
    cht.PivotLayout.PivotFields("P&L").CurrentPage = ComboBox2 'error here
    cht.PivotLayout.PivotTable.PivotFields("Sub P&L").CurrentPage = ComboBox1
    I appreciate everyones help, but do realize, I'm new to coding so I don't really understand all the advice that is being given.

  12. #12
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    Okay, instead of trying to update the chart, can I not just update the sheet that has the pivot table instead? So I set Combobox 1 and 2 on the first sheet, then have the pivot table in "sheet1" change. Here is the recorded macro

    Sheets("Sheet1").Select
        ActiveSheet.PivotTables("PivotTable10").PivotFields("P&L").CurrentPage = "PS"
        ActiveSheet.PivotTables("PivotTable10").PivotFields("Sub P&L").CurrentPage = _
            "AMI"
        Sheets("Opportunity Created").Select
    and here is what I tried:

     Private Sub ChooseButton_Click()
    
    Dim s As String
    Dim s1 As String
    
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    
    Dim chart1 As Chart
    
    s = ".csv Oppty Created to R1 Date L"
    s1 = "Sheet1"
    
    Set ws1 = Workbooks("Leading Indicators FW33.xls").Sheets(s1)
        
      ws1.PivotTables("PivotTable10").PivotFields("P&L").CurrentPage = ComboBox2
        ws1.PivotTables("PivotTable10").PivotFields("Sub P&L").CurrentPage = ComboBox1
    
    end sub
    I get run time error 1004: unable to set the default property of the pivotitem class at the bolded line.

  13. #13
    Registered User
    Join Date
    08-20-2009
    Location
    NC
    MS-Off Ver
    Excel 2000
    Posts
    21

    Re: run time error 9 : subscript out of range

    actually i think i fixed it guys, sorry for all the posts

+ 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