Consulting

Results 1 to 2 of 2

Thread: Coping data from one workbook to another based on specific criteria

  1. #1

    Exclamation Coping data from one workbook to another based on specific criteria

    Hello all,

    This is my first post and I have looked all over the internet to find an answer. I am new to VBA and coding so my terminology is not so great, so please bear with me. What I am trying to do is create a macro that will copy some data from one workbook and create a new workbook and paste that data in the new workbook then save the workbook. I want the macro to copy specific data. Below I have my data pasted so you can see what I am trying to do and I have also attached my spreadsheet if you need to look at that as well.

    What I am specifically trying to do is have the macro evaluate a certain date in column A (in this case 6/19) and a specific word in column B(Sales). If it meets both criteria ( 6/19 and Sales) I then need it to copy the entire column and paste into a new worksheet and save it. Right now when I execute the macro it just copies the first line (line 2) and then pastes it correctly.

    Sub mysales()
    Dim LastRow As Integer, i As Integer, erow As Integer
    LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
     
    For i = 2 To LastRow
     
    If Cells(i, 1) = "6/19" And Cells(i, 2) = "Sales" Then
     
    Range(Cells(i, 1), Cells(i, 50)).Select
    Selection.Copy
    Workbooks.Open Filename:="C:\Users\ryanb\Downloads\Nightbook.xlsm"
    Worksheets("Sheet2").Select
    erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
    ActiveSheet.Paste
    ActiveWorkbook.Save
    Application.CutCopyMode = False
    End If
    Next i
    End Sub
    Thank you in advance for you help!
    Ryno
    Attached Files Attached Files
    Last edited by mdmackillop; 07-04-2017 at 02:31 AM. Reason: Code tags added

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub vbax_59958_copy_rows_based_on_condition()
    
        With ThisWorkbook.Worksheets("Sheet1")
            .Cells(1).AutoFilter 1, "6/19"
            .Cells(1).AutoFilter 2, "Farmland"
            If .AutoFilter.Range.Rows.Count > 1 Then
                .UsedRange.Offset(1).SpecialCells(12).EntireRow.Copy
                Workbooks.Open "C:\Users\ryanb\Downloads\Nightbook.xlsm"
                With ActiveWorkbook
                    .Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial
                    .Save
                End With
            End If
            .AutoFilterMode = False
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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