+ Reply to Thread
Results 1 to 3 of 3

Nested if returning error 1004

Hybrid View

Guest Nested if returning error 1004 02-02-2006, 03:55 PM
Guest Re: Nested if returning error... 02-02-2006, 04:15 PM
Guest Re: Nested if returning error... 02-02-2006, 04:55 PM
  1. #1
    tom
    Guest

    Nested if returning error 1004

    This sub returns error 1004 when it hits the second if statement. Can
    you tell me why?

    ------------
    Sub addYears()

    Dim year1 As Integer
    Dim year2 As Integer

    For Each cell In Range("a1", "a3000")
    If cell.Value = cell.Offset(1, 0).Value Then
    year1 = cell.Offset(0, 4).Value
    year2 = cell.Offset(1, 4).Value
    cell.Offset(0, 5).Value = year1 + year2
    Else
    If cell.Value = cell.Offset(-1, 0).Value Then
    cell.Offset(0, 5).Value = "See Above"
    End If

    End If
    Next cell
    ----------

    Thanks

    -tom


    End Sub


  2. #2
    tom
    Guest

    Re: Nested if returning error 1004

    It works if the second if statement refers to something other than
    offset(-1,0), such as offset(1,0), then it works.
    Ideas?


  3. #3
    Charlie
    Guest

    Re: Nested if returning error 1004

    when your cell is on Row 1 your offset of -1 is an error
    Try this:

    For Each Cell In Range("a1", "a3000")
    If Cell.Value = Cell.Offset(1, 0).Value Then
    year1 = Cell.Offset(0, 4).Value
    year2 = Cell.Offset(1, 4).Value
    Cell.Offset(0, 5).Value = year1 + year2
    Else
    If Cell.Row > 1 Then
    If Cell.Value = Cell.Offset(-1, 0).Value Then
    Cell.Offset(0, 5).Value = "See Above"
    End If
    End If
    End If
    Next Cell


    "tom" wrote:

    > It works if the second if statement refers to something other than
    > offset(-1,0), such as offset(1,0), then it works.
    > Ideas?
    >
    >


+ 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