Closed Thread
Results 1 to 16 of 16

If Cell = X, Then Copy Entire Row Into A New Sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    04-04-2009
    Location
    N/A
    MS-Off Ver
    Excel 2003
    Posts
    24

    If Cell = X, Then Copy Entire Row Into A New Sheet

    Hi - as per title, could someone advise me how to do this please? Basically, I have a report that I use everyweek and split up into various sheets according to the criteria.

    So if any cell in column C contains "Apple", then I want it copied into a new s/s (with the headers). I basically have to do a number of iterations on this, can someone assist please? The main problem I invisage is asking Excel to copy the row into the new sheet underneath the last one.

    Many thanks, Jimmy.

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,080

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    You could try filtering on column C and manually copying and pasting the data into a new spreadsheet
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Registered User
    Join Date
    04-04-2009
    Location
    N/A
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Right, that's what I'm doing now. But I'm trying to get the vba coding in, to avoid the manual work.

  4. #4
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Need to clarify something.
    Is this a conditional behavior, ie. copy row to a new sheet IF certain criteria are met.

    Or do you have several rows worth of information which you would like new sheets made, with the headers of each being named after that row's name.
    =IF(AND(OR(BLONDE,BRUNETTE,REDHEAD),OR(MY PLACE,HER PLACE),ME),BOW-CHICKA-BOW-WOW,ANOTHER NIGHT ON THE INTERNET)

  5. #5
    Registered User
    Join Date
    04-04-2009
    Location
    N/A
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Conditional Behaviour. So firstly each sheet will carry the same set of headers.

    Then, if any cell in Column B contains "Apple", then the entire row needs to be copied into Sheet2 (one underneath each other after the header). If any cell in Column B contains "Orange", then the entire row needs to be copied into Sheet3, etc etc. I have a series of these I need to do.

    Thanks for any help.

  6. #6
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    An example workbook would be invaluable at this point.

    I *think* that you want to go through all of column b, and for every cell that has a corresponding worksheet with the same name, add that information to the next empty row of that worksheet. If there is NOT a corresponding worksheet, do you want a new one made to suit, and continue on from there?
    Last edited by mewingkitty; 04-20-2009 at 09:41 AM.

  7. #7
    Registered User
    Join Date
    04-04-2009
    Location
    N/A
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Here you go, so basically Sheet 1 is a mock-up of my extracted data. And the resulting three tabs are what I'd like generated from the macro (the Sheets are always pre-defined - or at least will be when I write the vba code).
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-06-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Here is an invaluable piece of code I frequently use. I take no credit for its existence.

    It doesn't do your job quite how you describe. Though it will split a database into sheets by column A. Either copy your column B to A, or change the code. So where it says A1, change to B1.

    I'm sure you'll get a perfect solution shortly; knowledge is abundant around here.

    Sub PagesByDescription()
    
    Dim rRange As Range, rCell As Range
    Dim wSheet As Worksheet
    Dim wSheetStart As Worksheet
    Dim strText As String
    
        Set wSheetStart = ActiveSheet
        wSheetStart.AutoFilterMode = False
        
        'Set a range variable to the correct item column
        Set rRange = Range("A1", Range("A65536").End(xlUp))
            
            'Delete any sheet called "UniqueList"
            'Turn off run time errors & delete alert
    
            On Error Resume Next
            Application.DisplayAlerts = False
            Worksheets("UniqueList").Delete
            'Add a sheet called "UniqueList"
    
            Worksheets.Add().Name = "UniqueList"
               
               'Filter the Set range so only a unique list is created
    
                With Worksheets("UniqueList")
                    rRange.AdvancedFilter xlFilterCopy, , _
                     Worksheets("UniqueList").Range("A1"), True
    
                     'Set a range variable to the unique list, less the heading.
    
                     Set rRange = .Range("A2", .Range("A65536").End(xlUp))
                End With
    
                
                On Error Resume Next
    
                With wSheetStart
                    For Each rCell In rRange
                      strText = rCell
                     .Range("A1").AutoFilter 1, strText
                        Worksheets(strText).Delete
                        'Add a sheet named as content of rCell
                        Worksheets.Add().Name = strText
                        'Copy the visible filtered range _
                        'default of Copy Method) and leave hidden rows
                        .UsedRange.Copy Destination:=ActiveSheet.Range("A1")
                        ActiveSheet.Cells.Columns.AutoFit
                    Next rCell
                End With
    
            With wSheetStart
                .AutoFilterMode = False
                .Activate
            End With
      
            On Error GoTo 0
    
            Application.DisplayAlerts = True
    
    End Sub

  9. #9
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Okies right on, we're on the same page.

    See attached file.

    If the sheet name is going to have "my" at the beginning of it, you could modify the code to read from a mid of 2 to the end, that'd match it to everything after your "my". Currently it looks for exact text matches, and copies over the corresponding data. If no such sheet exists, it moves on.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    04-04-2009
    Location
    N/A
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Hi - thanks for the help. The reason I wrote them as "MyOranges" is because the sheet name isn't going to be whatever the criteria of the cell is. They will take on pre-defined names which I can write into the VBA code pretty easily.

    Also, am I going mad, or is there no code attached to the file lol?

  11. #11
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Option Explicit
    
    Private Sub CommandButton1_Click()
    
    Dim wks As Worksheet
    Dim x As Integer
    Dim y As Integer
    Dim z As Integer
    
    x = 1
    y = 2
    z = 1
    
    Do While Cells(x, 2) <> vbNullString
    
    x = x + 1
    y = 2
    z = 1
    
    For Each wks In Worksheets
        If UCase(Cells(x, 2)) = UCase(wks.Name) Then
            Do While Worksheets(wks.Name).Cells(y, 2) <> vbNullString
                y = y + 1
            Loop
            Do While Cells(x, z) <> vbNullString
                Worksheets(wks.Name).Cells(y, z) = Cells(x, z)
                z = z + 1
            Loop
        End If
    Next wks
    
    Loop
    
    End Sub
    You're going mad.
    :D

    When in design mode, double click on the command button,
    or alternatively, just click on the "Visual Basic" button on the top left of the developer tab.

    Let me know if you can work with that or if we need to go another direction.

  12. #12
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Sounds like something more along the lines of the attached file may be what you're after.

    There are all kinds of ways to go about this. You could even have it pop up three boxes asking for the criteria for page one, then two, then three, and have it run the search from there.

    There's still al lot I don't know about how you want this to work. Do you want to clear the three targets each time a search is run, or would you like it to check for duplicates..? At the moment if you keep clicking the button, it'll keep adding to the list.
    Attached Files Attached Files

  13. #13
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Or to eliminate the need to hop back into the code every time:
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    05-01-2009
    Location
    nj usa
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Hi,
    I am trying to do something very similar. If you look at the attached spreadsheet you will see 6 tabs. Main, New Cust, Quote, etc.

    "Main" will be the sheet that everything is inputted. If there is a "Y" under new customer, I want the entire row to copy to the tab "New Cust". If there is an "X" under quote, struct design, or graphic design I want it under its approp. tab. The Lisa tab should take certain Sales-Person and copy entire row on the Lisa tab. The certain Sales-Persons are BS, PK, PB, PD.

    Any help will be greatly appreciated.
    Attached Files Attached Files

  15. #15
    Forum Contributor mewingkitty's Avatar
    Join Date
    09-29-2008
    Location
    Fort McMurray, Alberta, Canada
    MS-Off Ver
    Excel 2003
    Posts
    949

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    sry, haven't been on in a while.

    Please explain when you want it to update. Could make it re-copy them all every time you changed which cell is active, but that's going to slow your sheet down a lot.

    Personally I like buttons. I'd make it so that you check off/uncheck whatever you want and then have a button you press to run the update.

    So, what's the plan.

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: If Cell = X, Then Copy Entire Row Into A New Sheet

    Hello MewingKitty,

    I am closing this thread because Alfonso83 should have started a new thread and not posted a related problem in another member's thread. If you want to follow up on this, I suggested you send Alfonso83 a private message or visitor's message and go from there. Not blaming you for this, we (the mods) missed this one.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Closed 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