+ Reply to Thread
Results 1 to 5 of 5

Copy cell values with 'if' statement throws error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Unhappy Copy cell values with 'if' statement throws error

    I'm trying to copy cell values from the next sheet to the currently active sheet but the 'if' condition throws the following error.
    Any idea what I'm missing or how to define the variable to eliminate the error?
    Perhaps there is a better way to do this, any help would be appreciated.
    Run-time error 91
    Object or with block variable not set
    If ActiveSheet.Next.Range("B2") = "Name:" Then
    ActiveSheet.Cells(2, 3) = ActiveSheet.Next.Range("C2")
    ActiveSheet.Cells(3, 3) = ActiveSheet.Next.Range("C3")
    ActiveSheet.Cells(4, 3) = ActiveSheet.Next.Range("C4")
    ActiveSheet.Cells(5, 3) = ActiveSheet.Next.Range("C5")
    ActiveSheet.Cells(5, 6) = ActiveSheet.Next.Range("F5")
    End If
    Last edited by Mayweed; 02-19-2011 at 10:25 PM.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Copy cell values with 'if' statement throws error

    Try

    If Sheets(ActiveSheet.index +1).range("B2") = "Name:" Then

  3. #3
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Copy cell values with 'if' statement throws error

    Thanks for that but it does the same thing if I run the code on the last sheet.
    What I'm trying to do is check for the presence of a sheet after this one, if this is the last sheet... do nothing but if there is a sheet after this one... copy the cells.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Copy cell values with 'if' statement throws error

    try to add another condition to avoid going beyond sheets number:
    ...
    If ActiveSheet.Index < ActiveWorkbook.Sheets.Count Then
        If Sheets(ActiveSheet.Index + 1).Range("B2") = "Name:" Then
    ....

  5. #5
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Copy cell values with 'if' statement throws error

    I used watersev's code to check the sheet status and it works fine.
    Thanks to both of you,

+ 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