+ Reply to Thread
Results 1 to 32 of 32

Subscript out of Range error

Hybrid View

lloydgodin Subscript out of Range error 03-06-2013, 11:51 AM
Norie Re: Subscript out of Range... 03-06-2013, 12:07 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 12:11 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 12:19 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 01:02 PM
Norie Did you try the change I... 03-06-2013, 01:07 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 01:26 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 01:08 PM
Norie Re: Subscript out of Range... 03-06-2013, 01:32 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 01:34 PM
Norie Re: Subscript out of Range... 03-06-2013, 02:25 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 02:34 PM
Norie Re: Subscript out of Range... 03-06-2013, 02:38 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 02:45 PM
Norie Re: Subscript out of Range... 03-06-2013, 02:47 PM
lloydgodin Re: Subscript out of Range... 03-06-2013, 02:55 PM
lloydgodin Re: Subscript out of Range... 03-11-2013, 11:17 AM
lloydgodin Re: Subscript out of Range... 03-11-2013, 11:44 AM
lloydgodin Re: Subscript out of Range... 04-05-2013, 01:36 PM
xladept Re: Subscript out of Range... 04-05-2013, 02:00 PM
lloydgodin Re: Subscript out of Range... 04-05-2013, 02:18 PM
xladept Re: Subscript out of Range... 04-05-2013, 03:03 PM
lloydgodin Re: Subscript out of Range... 04-09-2013, 02:12 PM
xladept Re: Subscript out of Range... 04-09-2013, 03:33 PM
lloydgodin Re: Subscript out of Range... 04-10-2013, 08:02 AM
xladept Re: Subscript out of Range... 04-10-2013, 01:29 PM
lloydgodin Re: Subscript out of Range... 04-10-2013, 01:32 PM
xladept Re: Subscript out of Range... 04-10-2013, 01:45 PM
lloydgodin Re: Subscript out of Range... 04-16-2013, 03:49 PM
xladept Re: Subscript out of Range... 04-16-2013, 05:29 PM
lloydgodin Re: Subscript out of Range... 04-17-2013, 08:22 AM
xladept Re: Subscript out of Range... 04-17-2013, 12:05 PM
  1. #1
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Subscript out of Range error

    In the attached workbook I am getting a "SUBSCRIPT OUT OF RANGE" error on the following line.

    ActiveWorkbook.Sheets(vData(n, 2)).Range("A1:" & vData(n, 3) & "100").Select
    The data is present in 'ref' however, the variant clears out at some point with no explanation. Here is complete code as well.

    Public Sub ResetReview()
    '
    
    
    '******************************* BEGIN VARIABLE DEFINITIONS
    
        Dim x, lnCount As Integer, valid As Boolean
        Dim rdDataRange As Range
        Dim vData       As Variant
        Dim n           As Long
        Dim sName       As String
        Dim wks         As Worksheet
        Dim ref_table   As Range
                
    '******************************* END VARIABALE DEFINITIONS
        Application.StatusBar = "Resetting Fields..."
        Application.ScreenUpdating = False
        
        vData = ThisWorkbook.Worksheets("Ref").Range("ref_table").Value
        Set wks = ActiveSheet
        sName = wks.Parent.Name
        
        For n = LBound(vData) To UBound(vData)
            If InStr(sName, vData(n, 1)) Then
                If vData(n, 4) > 0 Then
                    If Len(Range("B" & vData(n, 4))) > 0 Then
                        Set rdDataRange = ActiveWorkbook.Sheets(vData(n, 2)).Range("A" & vData(n, 4) & _
                        ":" & vData(n, 3) & ActiveWorkbook.Sheets(vData(n, 2)).Cells(Rows.Count, "A").End(xlUp).Row + 1)
                        ActiveWorkbook.Sheets(vData(n, 2)).Range("A" & vData(n, 4)).Select
                        With rdDataRange
                            lnCount = 0
                            Do
                                lnCount = lnCount + 1
                                ActiveCell.Offset(1, 0).Select
                            Loop Until IsEmpty(ActiveCell.Offset(0, 1))
                        End With
                        Rows(vData(n, 4) & ":" & (lnCount + vData(n, 4))).Select
                        Selection.Delete Shift:=xlUp
                    End If
                End If
                    
                If vData(n, 5) > 0 Then
                    If Len(Range("B" & vData(n, 5))) > 0 Then
                        Set rdDataRange = ActiveWorkbook.Sheets(vData(n, 2)).Range("A" & vData(n, 5) & _
                        ":" & vData(n, 3) & ActiveWorkbook.Sheets(vData(n, 2)).Cells(Rows.Count, "A").End(xlUp).Row + 1)
                        ActiveWorkbook.Sheets(vData(n, 2)).Range("A" & vData(n, 5)).Select
                        With rdDataRange
                            lnCount = 0
                            Do
                                lnCount = lnCount + 1
                                ActiveCell.Offset(1, 0).Select
                            Loop Until IsEmpty(ActiveCell.Offset(0, 1))
                        End With
                        Rows(vData(n, 5) & ":" & (lnCount + vData(n, 5))).Select
                        Selection.Delete Shift:=xlUp
                    End If
                End If
    '       End If
    '    Next n
    '
    '    For n = LBound(vData) To UBound(vData)
    '        If InStr(sName, vData(n, 1)) Then
                ActiveWorkbook.Sheets(vData(n, 2)).Range("A1:" & vData(n, 3) & "100").Select
                For Each x In Selection.Cells
                    If x.Locked = False Then
                        If x.MergeCells Then
                            x.MergeArea.Name = "MergedCells"
                            Range("MergedCells").ClearContents
                        Else
                            Select Case x.NumberFormat
                                Case "General"
                                    x.ClearContents
                                Case "0"
                                    x.ClearContents
                                Case "mm/dd/yy;@"
                                    x.ClearContents
                                Case "$#,##0.00_);($#,##0.00)"
                                    x.Value = "0"
                            End Select
                        End If
                    End If
                Next
    '        End If
    '    Next n
    '
    '    For n = LBound(vData) To UBound(vData)
    '        If InStr(sName, vData(n, 1)) Then
                With wks.Range(vData(n, 3) + "1")
                    .Value = "FACS#"
                    Application.Goto .Cells
                End With
                Exit For
            End If
        Next n
        
        ActiveSheet.Calculate
        Application.ScreenUpdating = True
        Application.StatusBar = "Ready"
    End Sub
    Attached Files Attached Files
    Last edited by lloydgodin; 03-06-2013 at 12:06 PM. Reason: corrected attachment

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Subscript out of Range error

    I get a Select method of range... error not subscript out of range.

    If I change the loop to this, no error...
                For Each x In ActiveWorkbook.Sheets(vData(n, 2)).Range("A1:" & vData(n, 3) & "100").Cells
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    I am not even getting the error anymore. I'm not sure why or where...

    And here is the corrected attachment.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    And now I AM getting the same error that you did.
    When I changed to your code I get the SUBSCRIPT OUT OF RANGE error again.

  5. #5
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Since 'ref' is contained in an add-in I thought the section of code causing the problem might be this:

        vData = ThisWorkbook.Worksheets("Ref").Range("ref_table").Value
        Set wks = ActiveSheet
        sName = wks.Parent.Name
    Other sections that reference it work.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    Did you try the change I suggested?

  7. #7
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    The add-in cannot be attached but it is a file that contains the above code in a module, the tab REF and the following code in ThisWorkbook:

    Const msoControlPopup As Long = 10
    Const msoControlButton As Long = 1
    
    
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        On Error Resume Next
        Application.CommandBars("Worksheet Menu Bar").Controls("TEST CONTROLS v1.0").Delete
    
    End Sub
    
    Private Sub Workbook_Open()
        Dim cmbBar As Object
        Dim cmbControl As Object
         
        Set cmbBar = Application.CommandBars("Worksheet Menu Bar")
        Set cmbControl = cmbBar.Controls.Add(Type:=msoControlPopup, temporary:=True)
        With cmbControl
            .Caption = "&TEST CONTROLS v1.0"
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "RESET REVIEW"
                .OnAction = "ResetReview"
                .FaceId = 1098
            End With
            With .Controls.Add(Type:=msoControlButton)
                .Caption = "PRINT REVIEW"
                .OnAction = "PrintReview"
                .FaceId = 1098
            End With
        End With
    End Sub

  8. #8
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Yes. And I get the subscipt out of range error still.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Subscript out of Range error

    It probably is some sort of reference problem that's causing the error(s).

    Hard to tell/test though without the add-in.

  10. #10
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    The site wont let me upload it. However, it can be created with the 11:26 post and last attachment has the "REF" tab.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Subscript out of Range error

    Sorry, 11:26 post?

    Could you post it's number?

    PS Are the 2 files you uploaded different?

  12. #12
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Post #8. 11:26 was the time on it b/c I didn't see the post numbers.

    The file in post #3 contains the REF tab whereas post 1 did not.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Subscript out of Range error

    Post #8 was at 5:26 pm GMT.

    I'll have a look at the file later.

    You say it's an addin?

    Do you have a sample workbook to work with it, or will a blank one do?

  14. #14
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Different time zones, LOL.

    Yes the ResetTest.xls is the sample workbook and using the code from previous posts and the REF tab from ResetTest you can create the add-in. It more accurately reflects how I am doing things on this end.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Subscript out of Range error

    So what should I use as a sample workbook?

  16. #16
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    the ResetTest.xls is the sample workbook
    DOesn't matter what you put into as long as you insert lines at 13 and fill it with garbage data. And where it says FACS# put something else is.

  17. #17
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Threadbump

  18. #18
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    I think it has to do with the variant 'n.' It's running it 101 times and after the data is empty it'll give the subscript out of range error.

  19. #19
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    bumping this again.

  20. #20
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    Hi Lloyd,

    I was playing with it too and this worked for me:

    ActiveWorkbook.Sheets(CStr(vData(n, 2))).Range("A1:" & vData(n, 3) & "100").Select
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  21. #21
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Thank XL. I'm actually in the office (Camarillo, CA) and the files are back in Texas on my computer. I will try it when I get back home and let you know. Thank you again.

  22. #22
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    You're welcome!

  23. #23
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Nothing I am doing is working. So I zipped the addin, as well as an actual spreadsheet it would be used on.
    Last edited by lloydgodin; 04-10-2013 at 08:03 AM. Reason: removed attachments.

  24. #24
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    Hi Lloyd,

    I altered the add in code a little and it ran - see if it's good enough:

    For n = LBound(vData) To UBound(vData)
            If vData(n, 1) <> "" Then
            If InStr(sName, vData(n, 1)) Then
            ActiveWorkbook.Sheets(CStr(vData(n, 2))).Select
                ActiveWorkbook.Sheets(CStr(vData(n, 2))).Range("A1:" & vData(n, 3) & "100").Select
              
                For Each x In ActiveWorkbook.Sheets(vData(n, 2)).Range("A1:" & vData(n, 3) & "100").Cells
                    If x.Locked = False Then
                        If x.MergeCells Then
                            x.MergeArea.Name = "MergedCells"
                            Range("MergedCells").ClearContents
                        Else
                            Select Case x.NumberFormat
                                Case "General"
                                    x.ClearContents
                                Case "0"
                                    x.ClearContents
                                Case "mm/dd/yy;@"
                                    x.ClearContents
                                Case "$#,##0.00_);($#,##0.00)"
                                    x.Value = "0"
                            End Select
                        End If
                    End If
                Next
            End If: End If
        Next n

  25. #25
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    If you could highlight the changes and explain why that would be helpful to me. It is working perfectly. Thank you.

  26. #26
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    Hi Lloyd,

    I noticed that it was choking when the ref was null - so I just told it to ignore null ref records:

    For n = LBound(vData) To UBound(vData)
            If vData(n, 1) <> "" Then
            If InStr(sName, vData(n, 1)) Then
            ActiveWorkbook.Sheets(CStr(vData(n, 2))).Select
                ActiveWorkbook.Sheets(CStr(vData(n, 2))).Range("A1:" & vData(n, 3) & "100").Select
              
                For Each x In ActiveWorkbook.Sheets(vData(n, 2)).Range("A1:" & vData(n, 3) & "100").Cells
                    If x.Locked = False Then
                        If x.MergeCells Then
                            x.MergeArea.Name = "MergedCells"
                            Range("MergedCells").ClearContents
                        Else
                            Select Case x.NumberFormat
                                Case "General"
                                    x.ClearContents
                                Case "0"
                                    x.ClearContents
                                Case "mm/dd/yy;@"
                                    x.ClearContents
                                Case "$#,##0.00_);($#,##0.00)"
                                    x.Value = "0"
                            End Select
                        End If
                    End If
                Next
            End If: End If
        Next n
    Oh! and since the code uses the select feature for the range I Activated the Sheet.
    Last edited by xladept; 04-10-2013 at 01:31 PM.

  27. #27
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    OK. I can see that now. Not sure why it was returning null references though.
    Also what did the CStr() do?

    I'm pretty new to programming and hardly use what I've learned.

  28. #28
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    Hi Lloyd,

    That's a pretty sophisticate system for a newcomer - the CStr renders the worksheet name reference into a string and it's necessary because worksheet names are strings
    Last edited by xladept; 04-10-2013 at 09:52 PM.

  29. #29
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    Well I have had help from several people on here to get it to do what I want it to do.

    I did notice 1 error a week later. There are other workbooks that contain several worksheets (as opposed to the ER which contains just "emergency room"). The bill analysis clears ALL worksheets in a single workbook. Not really a problem except it only clears them and does not do the final step of putting "FACS#" into the cell and selecting the same. It only does that on the worksheet it's used on.

  30. #30
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    Hi LLoyd,

    Can you post the code or, even better, a sample spreadsheet with the fault??

  31. #31
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Subscript out of Range error

    I figured it out. The code you had provided went through the whole array n=lBound to uBound and used any worksheet name data contained in vData(n,2). All 100 worksheets. So I replaced

           ActiveWorkbook.Sheets(CStr(vData(n, 2))).Select
                ActiveWorkbook.Sheets(CStr(vData(n, 2))).Range("A1:" & vData(n, 3) & "100").Select
              
                For Each x In ActiveWorkbook.Sheets(vData(n, 2)).Range("A1:" & vData(n, 3) & "100").Cells
    with

            ActiveWorkbook.Sheets(wks.Name).Select
                ActiveWorkbook.Sheets(wks.Name).Range("A1:" & vData(n, 3) & "100").Select
              
                For Each x In ActiveWorkbook.Sheets(wks.Name).Range("A1:" & vData(n, 3) & "100").Cells
    Where wks = Activesheet so it only runs the code if the sheet is Active which is what I needed it to do.

  32. #32
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Subscript out of Range error

    Good! - Glad you got it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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