+ Reply to Thread
Results 1 to 7 of 7

Code working but on wrong sheet

Hybrid View

pauldaddyadams Code working but on wrong... 11-11-2011, 05:03 AM
royUK Re: Code working but on wrong... 11-11-2011, 05:37 AM
pauldaddyadams Re: Code working but on wrong... 11-11-2011, 06:08 AM
Alf Re: Code working but on wrong... 11-11-2011, 07:13 AM
pauldaddyadams Re: Code working but on wrong... 11-11-2011, 07:17 AM
Colin Legg Re: Code working but on wrong... 11-11-2011, 07:25 AM
pauldaddyadams Re: Code working but on wrong... 11-11-2011, 07:29 AM
  1. #1
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Code working but on wrong sheet

    Hi,

    The code below works however it copies it to the wrong sheet (the sheet you are viewing when the macro is ran).

    I think this is the part i need to amend:
    CopyToRange:=Range("A1"), Unique:=True
    Is there any way I can select the sheet I want to copy it to e.g.
    =Range(Sheets("macro"."A1")

    This is the code in full

    Sub Macro1()
    
    ' Macro1 Macro
    
        Sheets("May Sales.xls").Columns("D:D").AdvancedFilter Action:=xlFilterCopy, _
            CopyToRange:=Range("A1"), Unique:=True
        Sheets("macro").Range("A2:A" & Sheets("Macro").Cells(Rows.Count, "A").End(xlUp).Row).Copy
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        Sheets("Overview").Range("I4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    End Sub
    Last edited by pauldaddyadams; 11-11-2011 at 07:30 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Code working but on wrong sheet

    You need to add the sheet name before that part
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Code working but on wrong sheet

    How do I do that, I tried this but it didnt work (sheet name is Macro)

    CopyToRange:=Sheets("Macro").Range("A1"), Unique:=True

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Code working but on wrong sheet

    You could try:

    CopyToRange:=Range("Macro!A1"), Unique:=True
    Alf

  5. #5
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Code working but on wrong sheet

    Unfortunatly that didnt work

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Code working but on wrong sheet

    Hi Paul,

    Copying to a different sheet with the advanced filter is one of the rare occasions when you have to use the Worksheet.Select method. That is, you must ensure that the target sheet is the active sheet before you call Range.AdvancedFilter.

    Something like this (untested):

    Sub Macro1()
    
        ' Macro1 Macro
        
        Worksheets("Macro").Select    
    
        Worksheets("May Sales.xls").Columns("D:D").AdvancedFilter _
                            Action:=xlFilterCopy, _
                            CopyToRange:=Worksheets("Macro").Range("A1"), _
                            Unique:=True
                            
        Worksheets("macro").Range("A2:A" & Worksheets("Macro").Cells(Worksheets("macro").Rows.Count, "A").End(xlUp).Row).Copy
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        Worksheets("Overview").Range("I4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    
    End Sub
    Hope that helps,

    Colin

    RAD Excel Blog

  7. #7
    Valued Forum Contributor
    Join Date
    04-21-2005
    Location
    Southern England
    MS-Off Ver
    Excel for Office 365
    Posts
    1,702

    Re: Code working but on wrong sheet

    Excellent - that worked!! Thank you!

    I wish i never ever used advanced filter lol

+ 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