+ Reply to Thread
Results 1 to 2 of 2

Copy row to new sheet when Column has an "X"

Hybrid View

  1. #1
    Registered User
    Join Date
    06-22-2011
    Location
    London, ONtario
    MS-Off Ver
    Excel 2007
    Posts
    3

    Copy row to new sheet when Column has an "X"

    Hi there,

    I am completely new at writing macros/vbas.

    What I need is for when a column has an X, I need that row to be copied to a new sheet with the same name as the column.
    Some rows have multiple columns with X's so I need them to be copied respectively.

    EX: if Blessed Kateri and Cornerview have and X, I need that row to be copied to both the Blessed Kateri and Cornerview sheet.


    Thank you for any help on the matter
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Copy row to new sheet when Column has an "X"

    How close am I to what you want?
    Option Explicit
    
    Sub MainMacro()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Centralized")
    Dim lastrow As Long, lastcol As Long, icol As Long
    Dim icell As Range
    
    lastrow = ws1.Range("A" & Rows.Count).End(xlUp).Row
    lastcol = ws1.Cells(1, Columns.Count).End(xlToLeft).Column
    
    For icol = 3 To lastcol
        For Each icell In ws1.Range(ws1.Cells(2, icol), ws1.Cells(lastrow, icol))
            If icell.Value = "X" Then
                If Sheets(Sheets.Count).Name <> ws1.Cells(1, icol).Value Then
                    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = ws1.Cells(1, icol).Value
                    icell.EntireRow.Copy _
                        Destination:=Sheets(ws1.Cells(1, icol).Value).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                Else
                    icell.EntireRow.Copy _
                        Destination:=Sheets(ws1.Cells(1, icol).Value).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                End If
            End If
        Next icell
    Next icol
    
    End Sub

+ 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