+ Reply to Thread
Results 1 to 14 of 14

Copy marked rows to new sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Copy marked rows to new sheet

    I'm trying to get all the rows that are marked with an "x" in column B to be copied and placed into a new worksheet.

    I recorded the basic operation and got it doing what i'd like until the error "out of range" pops up (noted in code) :-/

    also, i want to remove the filter on the original sheet...

    The reason I want to do this in VBA is b/c i have a workbook with many worksheets and i'm eventually going to want this to run through each worksheet, doing the same thing automatically... is there a better way to go about it?

    thanks!!

    Sub sortbyX()
    
    'Application.ScreenUpdating = False
        
    'filter the above definited range by "x" in column B
    
        UsedRange.Select
        Selection.AutoFilter
        ActiveSheet.UsedRange.AutoFilter Field:=2, Criteria1:="x"
        Selection.SpecialCells(xlCellTypeVisible).Select
        Selection.Copy
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets("Sheet4").Select 'runtime error 9: out of range
        Sheets("Sheet4").Name = "Timeline"
        Range("A1").Select
        Application.CutCopyMode = False
        ActiveSheet.Paste
        'here i'd like to remove the filter from the original sheet again
    End Sub
    Attached Files Attached Files
    Last edited by lennoxem; 12-01-2011 at 04:23 AM.

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Copy marked rows to new sheet

    Hi,

    Try changing the sheet reference to index instead, like this
    And i've streamlined your code a bit

    Sub sortbyX()
    
    'Application.ScreenUpdating = False
        
    'filter the above definited range by "x" in column B
    
        UsedRange.AutoFilter
        ActiveSheet.UsedRange.AutoFilter Field:=2, Criteria1:="x"
        Selection.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.name = "Timeline"
        Application.CutCopyMode = False
        Sheets(4).Range("A1").PasteSpecial
        Sheets(1).ShowAllData ' change reference to the actual sheet
    End Sub
    Please take time to read the forum rules

  3. #3
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows to new sheet

    Great! It works as far as creating the new sheet, and naming it properly. However, now an error occurs when it tries to paste into the new sheet. Should I try to put this into option explicit? (esp. since the rest of the workbook this will be integrated into always has option explicit as well....) I'm not exactly sure how to do that...

    Sub sortbyX()
    
    'Application.ScreenUpdating = False
        
        UsedRange.AutoFilter
        ActiveSheet.UsedRange.AutoFilter Field:=2, Criteria1:="x"
        Selection.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.Name = "Timeline"
        Application.CutCopyMode = False
        Sheets(1).Range("A1").Paste 'error appears here "object doesn't support this property or method"
        Sheets(2).ShowAllData
    End Sub
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Copy marked rows to new sheet

    Change this

    Sheets(1).Range("A1").Paste
    To
    Sheets(1).Range("A1").PasteSpecial

  5. #5
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows to new sheet

    the error now reads "application-defined or object-defined error" in the same place :-/

  6. #6
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Copy marked rows to new sheet

    Sorry my error

    Try this

    Sub sortbyX()
    
    'Application.ScreenUpdating = False
        
        UsedRange.AutoFilter
        Sheets(2)..UsedRange.AutoFilter Field:=2, Criteria1:="x"
        Sheets(2).UsedRange.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.Name = "Timeline"
        Application.CutCopyMode = False
        Sheets(1).Range("A1").PasteSpecial
        Sheets(2).ShowAllData
    End Sub

  7. #7
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows to new sheet

    thanks for sticking with me

    the error is still in the same place in the code. I had to change the sheet reference number to 1 in the beginning, becuase at first, it is the first sheet. the timeline sheet is inserted at the beginning, which changes the reference number of any sheet that will follow it...

    
    Sub sortbyX()
    
    'Application.ScreenUpdating = False
        
        UsedRange.AutoFilter
        Sheets(1).UsedRange.AutoFilter Field:=2, Criteria1:="x"
        Sheets(1).UsedRange.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.Name = "Timeline"
        Application.CutCopyMode = False
        Sheets(1).Range("A1").PasteSpecial
        Sheets(2).ShowAllData
    End Sub

  8. #8
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Copy marked rows to new sheet

    Your changing around the sheet references, what sheet holds the values to be copied and wich sheet should it be pasted on?

  9. #9
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows to new sheet

    i know... it's confusing.... in the beginning, the sheet with the information is sheet 1, then when the new sheet is added, it becomes second because the new sheet is first. I tried it out like this:
    Sub sortbyX()
    
    'Application.ScreenUpdating = False
        
        UsedRange.AutoFilter
        Sheets(1).UsedRange.AutoFilter Field:=2, Criteria1:="x"
        Sheets(1).UsedRange.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.Name = "Timeline"
        Application.CutCopyMode = False
        Sheets(2).Range("A1").PasteSpecial
        Sheets(1).ShowAllData
    End Sub
    and it tried to copy onto the sheet where it copied FROM in the first place...

  10. #10
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Copy marked rows to new sheet

    The try this instead

    Sub sortbyX()
    
    'Application.ScreenUpdating = False
    
    Dim thisWS as Worksheet
    Set thisWS = Sheets(1)
        
        thisWS.UsedRange.AutoFilter Field:=2, Criteria1:="x"
        thisWS.UsedRange.SpecialCells(xlCellTypeVisible).Copy
        Sheets.Add.Name = "Timeline"
        Application.CutCopyMode = False
        Sheets("Timeline").Range("A1").PasteSpecial
        thisWS.ShowAllData
    End Sub

  11. #11
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows to new sheet

    awesome, the reference problem is solved, and i figured out the error as well... turns out this was the culprit:
     Application.CutCopyMode = False
    ...

    Thank you for your help now to make it work in the real workbook....
    Last edited by lennoxem; 11-24-2011 at 08:09 AM.

  12. #12
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows from other sheets to current sheet

    I'm trying to integrate this function into the main workbook now, and I'm running into problems with defined objects (I think).

    This is the code as it stands:
    Option Explicit
    
    
    Private Sub Worksheet_Activate() 'when the "Timeline" worksheet is activated
    
    Dim ws As Worksheet
    Dim lngLast As Long
    Const strForbiddenWorksheetNames As String = "ALL#FTW#APP#ACC"
    'Application.ScreenUpdating = False
    
    Me.Cells.Clear
    Worksheets("FTW SS12").Rows(1).Copy _
      Destination:=Me.Rows(1)
    
    
    For Each ws In ThisWorkbook.Worksheets
        If InStr(1, strForbiddenWorksheetNames, ws.Name, vbTextCompare) = 0 Then
         
            lngLast = ws.Cells(Rows.Count, 1).End(x1up).Row 'error arrives here saying "x1up" isn't defined, however it also was not in the code I copied it from
            ws.Range("A2:AB" & lngLast).AutoFilter Field:=2, Criteria1:="x"
            ws.Range("A2:AB" & lngLast).Cells.Copy _
                Destination:=Me.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            ws.ShowAllData
        End If
        Next ws
            
        
        Me.UsedRange.Sort Key1:=Me.Range("I2"), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
                    
    End Sub
    I combined what I learned in this thread so far with some code that has been working throughout the rest of the workbook quite well (checking through the tabs for names it recognizes, copying the whole worksheet and pasting into the current sheet below the existing data). However, now I want it to sort for "x" before copying and pasting into the current sheet... ideas?
    Attached Files Attached Files

  13. #13
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Copy marked rows to new sheet

    The error you get is because it s

    xlUp, not x1up

  14. #14
    Registered User
    Join Date
    11-07-2011
    Location
    Germany
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy marked rows to new sheet

    ah, silly mistake.... i guess that's what happens when you're just learning as you go along!

+ 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