+ Reply to Thread
Results 1 to 8 of 8

Copy range from the worksheet preceding the active worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    01-14-2013
    Location
    Maine
    MS-Off Ver
    Excel 2007
    Posts
    53

    Copy range from the worksheet preceding the active worksheet

    I need to copy a range of cells from a worksheet before the active worksheet. I can't use the name of the worksheet or its index number because these two things are always changing. So I need to reference the worksheet I want to copy from using only the active worksheet. My code looks like this:

    Sub Macro6()
        Sheets("1").Select
        Range("C11:E11").Copy
        Sheets("2").Select
        Range("C11:E11").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    End Sub
    So sheet "2" is my active and sheet "1" is the one I need the cells from. But I need to reference sheet 1 using the active sheet ("2")

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

    Re: Copy range from the worksheet preceding the active worksheet

    The only way I can think of getting the previous sheet woud be to use the index of the current sheet and subtract one.

    
    Sheets(ActiveSheet.Index).Range("C11:E11").Copy
    
    ActiveSheet.Rnge("C11:E11").PasteSpecial xlPasteValues
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-14-2013
    Location
    Maine
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Copy range from the worksheet preceding the active worksheet

    I am not getting how I would do that by looking at your code. This copies from the active worksheet:

    Sheets(ActiveSheet.Index).Range("C11:E11").Copy
    So how do you subtract 1 from the index?

    Thank you so much for your time.

  4. #4
    Registered User
    Join Date
    01-14-2013
    Location
    Maine
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Copy range from the worksheet preceding the active worksheet

    I apologize, I figured it out. I was able to set both worksheets as variables like this:

    'Create New Tab, Clear Previous Repeat Data (if any)
    ActiveWorkbook.ActiveSheet.Unprotect
    
    Dim wks As Worksheet
    Set wks = ActiveSheet
    
    ActiveWorkbook.ActiveSheet.Copy after:=ActiveWorkbook.ActiveSheet
    
    Dim wks2 As Worksheet
    Set wks2 = ActiveSheet
    
    wks.Select
        Range("C11:E20").Copy
    wks2.Select
        Range("C11:E20").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
     Set wks = Nothing
     Set wks2 = Nothing
    Last edited by davidrias; 03-31-2013 at 07:42 PM. Reason: making easier to read

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

    Re: Copy range from the worksheet preceding the active worksheet

    Oops, forgot about Previous and Next.
    
    Set wsPrev = ActiveSheet.Previous
    
    Set wsNext = ActiveSheet.Next

  6. #6
    Registered User
    Join Date
    01-14-2013
    Location
    Maine
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Copy range from the worksheet preceding the active worksheet

    Oh perfect! Thanks, it seemed like a simple and reasonable thing to be able to do.

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

    Re: Copy range from the worksheet preceding the active worksheet

    Sorry it should have been this.
    Sheets(ActiveSheet.Index-1).Range("C11:E11").Copy

  8. #8
    Registered User
    Join Date
    01-14-2013
    Location
    Maine
    MS-Off Ver
    Excel 2007
    Posts
    53

    Re: Copy range from the worksheet preceding the active worksheet

    Thank you I will tuck all of this away for future reference.

+ 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