Results 1 to 7 of 7

Run-time error '9' : Subscript out of range . Some data works, Some do not work

Threaded View

  1. #1
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Run-time error '9' : Subscript out of range . Some data works, Some do not work

    Hi,

    I have macro problem "Run-time error '9' : Subscript out of range".
    The macro works in some data, some data need macro changes, some data do not work at all.

    The excel file in attachment. have 3 sheet, "Monday", "Tuesday" and "Friday".

    1) Sheet "Monday" : Works with original macro ( no changes).
    2) Sheet "Tuesday" : Works with changes
    For i = 1 To UBound(a, 1)
    to
    For i = 1 To UBound(a, 1) - 1
    3) Sheet "Friday" : Not work with original macro or changes.

    What is macro do is finding number pattern of Plus-Zero-Minus (+0-) then assign "1" at the start of pattern.
    The data, original macro and change macro included in file excel.

    Excel File : https://www.mediafire.com/file/12p3t...acro.xlsb/file

    Original Code:
    Sub test_ori()
        Dim a, b, i As Long, ii As Long
        With Range("e3", Range("e" & Rows.Count).End(xlUp))
            a = .Value: ReDim b(1 To UBound(a, 1), 1 To 1)
            For i = 1 To UBound(a, 1)
                If a(i, 1) > 0 Then
                    ii = 1
                    Do While a(i + ii, 1) = 0
                        ii = ii + 1
                    Loop
                    If (ii > 1) * (a(i + ii, 1) < 0) Then b(i, 1) = 1
                    i = i + ii - 1
                End If
            Next
            .Columns(3) = b
        End With
    End Sub
    Change Code:
    Sub test_minus1()
        Dim a, b, i As Long, ii As Long
        With Range("e3", Range("e" & Rows.Count).End(xlUp))
            a = .Value: ReDim b(1 To UBound(a, 1), 1 To 1)
            For i = 1 To UBound(a, 1) - 1
                If a(i, 1) > 0 Then
                    ii = 1
                    Do While a(i + ii, 1) = 0
                        ii = ii + 1
                    Loop
                    If (ii > 1) * (a(i + ii, 1) < 0) Then b(i, 1) = 1
                    i = i + ii - 1
                End If
            Next
            .Columns(3) = b
        End With
    End Sub
    Last edited by your; 05-30-2024 at 10:42 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Subscript out of Range ? Run time error 9
    By Skullypots in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-20-2024, 10:32 AM
  2. Error code "run time error 9: subscript out of range
    By iBennett93 in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 06-28-2021, 05:03 AM
  3. Run Time Error 9: Subscript out of range
    By Pauly723 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-05-2015, 06:09 PM
  4. Run-time error '9': Subscript out of range
    By ndsutherland in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-26-2014, 01:43 AM
  5. Run-Time error '9' Subscript out of range
    By wahi00 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2014, 09:56 PM
  6. [SOLVED] Run-time error '9': subscript out of range - error occurs on multiple computers except one
    By BrettE in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2014, 11:19 PM
  7. [SOLVED] Run-time Error '9': Subscript out of Range
    By Veena Anoohya in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-19-2014, 05:06 AM

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