+ Reply to Thread
Results 1 to 3 of 3

Copy rows in a new sheet with a condition

  1. #1
    Registered User
    Join Date
    04-12-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    12

    Copy rows in a new sheet with a condition

    Hi there, I'm looking to copy several thousands rows from sheet1 to sheet2, with the condition that column H has a value of "04T". I've found this example (see below) in a previous post, but I have trouble translating it to my own case. First of all, I need to copy data to a new sheet in the same workbook, not a new one like below. I am also unsure how to properly write the name of workbooks and worksheets, and I would need an example (like, how to properly insert the workbook called "Truck Rates" and the sheet called "Truck1".

    Thanks!

    Sub Extract_Data()
    'this macro assumes that your first row of data is a header row.
    'will copy a row from one worksheet, to another blank workbook
    'IF there is a 0 in column N
    'Variables used by the macro
    Application.ScreenUpdating = False
    Dim FilterCriteria
    Dim CurrentFileName As String
    Dim NewFileName As String

    'Get the current file's name
    CurrentFileName = ActiveWorkbook.Name
    'Select Range
    '(note you can change this to meet your requirements)
    Range("A1:AS3000").Select
    'Apply Autofilter
    Selection.AutoFilter
    FilterCriteria = 0
    'NOTE - this filter is on column N (field:=14), to change
    'to a different column you need to change the field number
    Selection.AutoFilter field:=14, Criteria1:=FilterCriteria
    'Select the visible cells (the filtered data)
    Selection.SpecialCells(xlCellTypeVisible).Select
    'Copy the cells
    Selection.Copy
    'Open a new file
    Workbooks.Add Template:="Workbook"
    'Get this file's name
    NewFileName = ActiveWorkbook.Name
    'Make sure you are in cell A1
    Range("A1").Select
    'Paste the copied cells
    ActiveSheet.Paste
    'Clear the clipboard contents
    Application.CutCopyMode = False
    'Go back to the original file
    Workbooks(CurrentFileName).Activate
    'Clear the autofilter
    Selection.AutoFilter field:=1
    'Take the Autofilter off
    Selection.AutoFilter
    'Go to A1
    Range("A1").Select
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Copy rows in a new sheet with a condition

    attach a sample file with 20 rows and desired result
    If solved remember to mark Thread as solved

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Copy rows in a new sheet with a condition

    Does this help any?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

+ 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