+ Reply to Thread
Results 1 to 8 of 8

Copy rows to new sheet, based on y/n value in 1st column

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2014
    MS-Off Ver
    Excel 2010
    Posts
    4

    Copy rows to new sheet, based on y/n value in 1st column

    Hi All,
    I'm pulling my hair out (partly cause my VBA skills are nill, and because I know this can be easily done).

    I have a single sheet that lists available items, prices, etc. When doing quotes, you simply put y/n in Col:A for each item. I then want a simple macro that will copy all of the rows with "yes" into a second sheet. Sample data with end result attached.

    I did try and butcher this macro to do what I want, and whilst it does copy data across, it's not really working. Firstly as I think it is searching along a row and copy columns, whereas I need to search a column and then copy rows.

    Sub copyQuote()
        Dim LC As Long, LR As Long, FC As Long, c As Range, rData As Range
        Application.ScreenUpdating = False
        LC = ActiveSheet.Range("A4").End(xlToLeft).Column
        For Each c In Range(Cells(4, 2), Cells(4, LC))
            If c.Value = "y" Then
                FC = Sheets("License").Range("A4").End(xlToLeft).Offset(, 1).Column
            Else
                FC = 1
            End If
            LR = Cells(Rows.Count, c.Column).End(xlUp).Row
            Set rData = Application.Union(c.Offset(1), c.Offset(13).Resize(LR - 5))
            rData.Copy Sheets("Legrand").Cells(1, FC)
        Next c
        Application.ScreenUpdating = True
    End Sub
    Thanx in advance.
    sample_data.xls

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Using autofilter will be quicker than looping through I think:
    Sub macro_1()
    Dim lrow
    lrow = Range("A" & Rows.Count).End(xlUp).Row
    Range("A4:E" & lrow).AutoFilter 1, "y"
    Range("A1:E" & lrow).Copy Sheets("Sheet1").Range("A1")
    End Sub

  3. #3
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Try this attached file
    Attached Files Attached Files
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  4. #4
    Registered User
    Join Date
    05-08-2014
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Ok, tested on a dataset......

    ragulduy - It filters the data, but doesn't seem to copy it. I have changed the "sheet" name to match my data..... Though, it did actually make me think and forehead slap myself. I could just apply a manual filter to column A!

    Naveed - This works great. Do have one question, my actual data has formulas in the cells. Could the macro copy the 'values' rather than the formulas?

    Cheers,

  5. #5
    Registered User
    Join Date
    05-08-2014
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Fantastic guys, thanx for the quick replies. I knew it fairly straight forward, for those who know!

    Will give it a go.

    Cheers,

  6. #6
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Glad we could help

    1. the Solution helps you, Click * to Add Reputation and leave ur comments
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  7. #7
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Just replace value below code with that existing code

    Sub copy_data()
    Dim DSht As Worksheet, RSht As Worksheet, LR As Long, FR As Long
    Set DSht = Sheets("Data")
    Set RSht = Sheets("Result")
    LR = DSht.Range("A" & Rows.Count).End(xlUp).Row
    For i = 4 To LR
        If DSht.Range("A" & i).Value = "y" Then
            FR = RSht.Range("A" & Rows.Count).End(xlUp).Row + 1
            DSht.Range("B" & i & ":" & "E" & i).Copy
                RSht.Range("A" & FR).PasteSpecial xlPasteValues
        End If
    Next
    Application.CutCopyMode = False
    MsgBox "Done !"
    End Sub

  8. #8
    Registered User
    Join Date
    05-08-2014
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Copy rows to new sheet, based on y/n value in 1st column

    Fantastic.... Thanx Naveed!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy rows to new sheet based on value in Column 1
    By lzrdlvr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-12-2013, 09:15 AM
  2. Copy rows into new sheet based on cell/column data
    By StagemanGraham in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-13-2012, 08:11 PM
  3. copy rows to new sheet based on column value..
    By wardga in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-29-2010, 07:44 AM
  4. [SOLVED] MACRO - copy rows based on value in column to another sheet
    By Michael A in forum Excel General
    Replies: 1
    Last Post: 03-04-2005, 11:06 PM
  5. [SOLVED] MACRO - copy rows based on value in column to another sheet
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-04-2005, 09:06 PM

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