+ Reply to Thread
Results 1 to 3 of 3

Copy row into other workbook if condition is met

Hybrid View

  1. #1
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Copy row into other workbook if condition is met

    Hey,
    What I'm trying to acheive is following:
    In the activesheet I want to copy a given row if the K column is non-empty and column L is empty. The row is copied into first empty row in another worksheet (C:\Users\bruger\Desktop\Excel\nytark.xlsx). The sheet, from which the data is copied, has a header in first row, and the copying should therefore start from second row. Thank you in advance for your help and effort!
    The code I have so far, found mostly at the forum and web, is following:

    Sub CopyRows()
    Dim wks As Worksheet
    Dim wbkPasteTo As Workbook
    Dim rngPasteTo As Range
    Dim rngFound As Range
    Dim rngFirst As Range
    Dim rngFoundAll As Range
    Dim rngToSearch As Range
    
    Set wks = Sheets("Sheet1")
    Set rngToSearch = wks.Columns("K")
    Set rngFound = rngToSearch.Find(What:="*", LookAt:=xlWhole)
    If rngFound Is Nothing Then
    MsgBox "Sorry Nothing to Move"
    ElseIf rngFound Is Not Nothing And rngFound.Offset(0, 1) = "" Then 'if this line is reduced to "Else" the code works, _ 
    but without the desired condition
    Set rngFoundAll = rngFound.EntireRow
    Set rngFirst = rngFound
    Do
    Set rngFoundAll = Union(rngFoundAll, rngFound.EntireRow)
    Set rngFound = rngToSearch.FindNext(rngFound)
    Loop Until rngFound.Address = rngFirst.Address
    On Error GoTo OpenBook
    Set wbkPasteTo = Workbooks("nytark.xlsx")
    On Error GoTo 0
    Set rngPasteTo = wbkPasteTo.Sheets("Ark1").Cells(Rows.Count, _
    "A").End(xlUp).Offset(1, 0)
    rngFoundAll.Copy rngPasteTo
    End If
    Exit Sub
    OpenBook:
    Workbooks.Open ("C:\Users\bruger\Desktop\Excel\nytark.xlsx")
    Resume
    End Sub
    Attached Files Attached Files
    Last edited by timtim89; 03-16-2012 at 05:33 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Copy row into other workbook if condition is met

    Try this,
    The assumption is that the workbook is closed when you run the code.
    It will copy the filtered data, open the workbook, paste the filtered data to the next empty row, then save and close the workbook.
    Sub FilterData()
    
        Dim Rws As Long, Rng As Range
    
        Rws = Cells(Rows.Count, "A").End(xlUp).Row
        Set Rng = Range(Cells(1, 1), Cells(Rws, 13))
        
        Application.ScreenUpdating = False
    
        Rng.AutoFilter Field:=11, Criteria1:="<>"
        Rng.AutoFilter Field:=12, Criteria1:="="
    
        Rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
        Workbooks.Open Filename:="C:\Users\davesexcel\Downloads\nytark.xlsx"
        Range("A65536").End(xlUp).Offset(1, 0).Select
        ActiveSheet.Paste
        ActiveWorkbook.Close True
        Rng.AutoFilter
    
    End Sub

  3. #3
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: Copy row into other workbook if condition is met

    Works great! you just saved my day

+ 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