Results 1 to 7 of 7

Thread: VBA code to move row to new spreadsheet

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0

    VBA code to move row to new spreadsheet

    I want to have this code wopy over all rows with a "Y" in column K to a new spreadsheet. I have this so far but it will only bring over the first row if it is marked. I need to get this to move over all rows marked even if the first row is not marked. Any help is appreciated. Thanks

    Code:
    Dim colLetter As String, SavePath As String
        Dim lastValue As String
        Dim wb As Workbook
        Dim lng As Long
        Dim currentRow As Long
        colLetter = "K"
        SavePath = ThisWorkbook.Path
            With ThisWorkbook.Worksheets(1)
            .Cells.AutoFilter field:=.Cells(1, colLetter).Column, Criteria1:="Y"
            lng = .Cells(.Rows.Count, colLetter).End(xlUp).Row
            Set wb = Application.Workbooks.Add(xlWorksheet)
            .Rows(1 & ":" & lng).Copy wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp)
            .AutoFilterMode = False
    Last edited by Admin; 06-10-2013 at 10:52 PM. Reason: code tag added

  2. #2
    Member
    Join Date
    Jun 2013
    Posts
    93
    Rep Power
    12
    Code:
        .Rows(2 & ":" & lng).Copy wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp)
        .AutoFilterMode = False

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313859#p313859
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313855#p313855
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313848#p313848
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313843#p313843
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313792#p313792
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313771#p313771
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313767#p313767
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313746#p313746
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313744#p313744
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313741#p313741
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313622#p313622
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313575#p313575
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313573#p313573
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313563#p313563
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313555#p313555
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533
    https://www.eileenslounge.com/viewtopic.php?f=39&t=40265&p=313468#p313468
    https://www.eileenslounge.com/viewtopic.php?f=42&t=40505&p=313411#p313411
    https://www.eileenslounge.com/viewtopic.php?f=32&t=40473&p=313384#p313384
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40501&p=313382#p313382
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40501&p=313380#p313380
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40501&p=313378#p313378
    https://www.eileenslounge.com/viewtopic.php?f=32&t=40473&p=313305#p313305
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40455&p=313035#p313035
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312889#p312889
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 03-01-2024 at 02:14 PM.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    That didn't work. All I am getting is a blank new workbook except when all of the "Y" are at the top in order. As soon as it hits an "N" it stops pulling over.

  4. #4
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Does this code do what you want (note that I removed some of the variables you declared which were not needed for this code, so if you need them later, you will have to Dim them again)...
    Code:
    Dim ColLetter As String, SavePath As String
    Dim VisibleRows As Range, WS As Worksheet, wbOld As Workbook, wbNew As Workbook
    ColLetter = "K"
    Set wbOld = ThisWorkbook
    Set wbNew = Application.Workbooks.Add(xlWorksheet)
    Set WS = wbOld.Worksheets(1)
    WS.Rows(1).Insert
    WS.Cells.AutoFilter field:=WS.Cells(1, ColLetter).Column, Criteria1:="Y"
    WS.AutoFilter.Range.SpecialCells(xlVisible).Copy wbNew.Worksheets(1).Range("A1")
    WS.AutoFilterMode = False
    WS.Rows(1).Delete

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    This works great except it does not bring over to column headers. Can you help with that? I have almost destroyed the macro. Thanks

  6. #6
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by cdurfey View Post
    This works great except it does not bring over to column headers. Can you help with that? I have almost destroyed the macro. Thanks
    Not sure what you mean by "I have almost destroyed the macro", but this modified version of what I posted earlier should do what I think you want...
    Code:
    Dim ColLetter As String, SavePath As String
    Dim VisibleRows As Range, WS As Worksheet, wbOld As Workbook, wbNew As Workbook
    ColLetter = "K"
    Set wbOld = ThisWorkbook
    Set wbNew = Application.Workbooks.Add(xlWorksheet)
    Set WS = wbOld.Worksheets(1)
    WS.Cells.AutoFilter field:=WS.Cells(1, ColLetter).Column, Criteria1:="Y"
    WS.AutoFilter.Range.SpecialCells(xlVisible).Copy wbNew.Worksheets(1).Range("A1")
    WS.AutoFilterMode = False

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    21
    Rep Power
    0
    That works perfect. Thanks

Similar Threads

  1. Replies: 4
    Last Post: 07-02-2013, 11:32 AM
  2. Excel VBA Code to Add New Sheets
    By cdurfey in forum Excel Help
    Replies: 1
    Last Post: 06-25-2013, 08:05 AM
  3. Replies: 7
    Last Post: 05-17-2013, 10:38 PM
  4. Replies: 1
    Last Post: 02-14-2013, 12:09 PM
  5. Vba Code to find value and paste on certain row
    By jwitte in forum Excel Help
    Replies: 3
    Last Post: 11-28-2012, 08:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •