+ Reply to Thread
Results 1 to 5 of 5

Application-Defined or Object-Defined Error

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2013
    Location
    Virginia US
    MS-Off Ver
    Excel 2010
    Posts
    7

    Application-Defined or Object-Defined Error

    Evening ladies & gents,

    I got this piece of code used to run fine but I started getting the above message.

    Sub DetermineRisk()
    
    Dim i As Integer
    Dim MyLastRow As Long
    Dim MyVar As Double
    Dim MyChange As Double
    ThisWorkbook.Activate
    For i = 4 To ActiveWorkbook.Worksheets.Count
        Sheets(i).Activate
        MyLastRow = Cells(Application.Rows.Count, 16).End(xlUp).Row
        MyVar = Cells(MyLastRow, 16).Offset(-1, 0)
        MyChange = Cells(MyLastRow, 16).Offset(0, 0)
        
        If Abs(MyVar) > 25000000 And Abs(MyChange) > 0.1 Then
            ActiveWorkbook.Sheets(ActiveSheet.Name).Tab.ColorIndex = 3
            
        Else
            ActiveWorkbook.Sheets(ActiveSheet.Name).Tab.ColorIndex = 4
        End If
    Next i
    
    End Sub
    The colored rows are the ones that have the issue.
    Im guessing it has something to do with the Cells reference.
    Any assistance would be much appreciated.

    Cheers

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Application-Defined or Object-Defined Error

    Why bother with the
    .Offset(0,0)
    I don't really understand that line of code. Couldn't it just be
    MyChange = Cells(MyLastRow,16)
    At any rate, the following didn't give me any errors:
    Sub DetermineRisk()
    
    Dim i As Integer
    Dim MyLastRow As Long
    Dim MyVar As Double
    Dim MyChange As Double
    ThisWorkbook.Activate
    For i = 4 To ActiveWorkbook.Worksheets.Count
        With Sheets(i)
            MyLastRow = Sheets(i).Cells(Sheets(i).Rows.Count, 16).End(xlUp).Row
            MyVar = Sheets(i).Cells(MyLastRow, 16).Offset(-1, 0)
            MyChange = Sheets(i).Cells(MyLastRow, 16).Offset(0, 0)
        
            If Abs(MyVar) > 25000000 And Abs(MyChange) > 0.1 Then
                Sheets(i).Tab.ColorIndex = 3
            
            Else
                Sheets(i).Tab.ColorIndex = 4
            End If
        End With
    Next i
    
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    03-26-2013
    Location
    Virginia US
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Application-Defined or Object-Defined Error

    Yes, Mordred indeed that line of code does not make sense.
    I copied pasted from the above line thinking that its one cell down in the same column.
    I ran the script again seems to be working.

    Thank you for your time.

  4. #4
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: Application-Defined or Object-Defined Error

    Hi Chinpunk

    I tested your code and found out that the problem occurs when MyLastRow variable = 1, because then MyVar tries to refer to row No.... 0, which obviously doesn't exist.
    I think this can be fixed using this:
    On Error Resume Next
    MyVar = Sheets(i).Cells(MyLastRow, 16).Offset(-1, 0)
            MyChange = Sheets(i).Cells(MyLastRow, 16).Offset(0, 0)
    On Error GoTo 0

  5. #5
    Registered User
    Join Date
    03-26-2013
    Location
    Virginia US
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Application-Defined or Object-Defined Error

    Hey Sbarro,

    Thanks for your reply.
    When i was hovering my mouse over the MyLastRow I saw the =1 value.
    I will give your code a try in case that error shows up again.
    By the way im using excel 2010 version.

    Thank you for you time.

+ 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