Question

I'm currently creating a new workbook on the fly and was wondering how you copy across data if certain conditions are met i.e. in this case if Primary Contact = John and Primary Device = Desktop then copy across the ID, First Name, Last Name and Email columns into the new workbook that was just created under column/row A11 onwards, B11 onwards, C11 onwards, D11 onwards (depending on how much data is pulled). Is this possible?

enter image description here

This is my code so far:

Sub NewWb()

' Create new workbook with a pre-defined name
Workbooks.Add
NewBook = ActiveWorkbook.Name
ThisWorkbook.Activate
Workbooks(NewBook).Activate

' Set bg to white
ActiveWorkbook.Sheets("Sheet1").Cells.Interior.ColorIndex = 2

' Sanity test
'MsgBox (ThisWorkbook.Sheets("Master").Cells(3, 3).Value)
'MsgBox (ActiveWorkbook.Sheets("Sheet1").Cells(7, 1).Value)

' Get last row
LastRow = ThisWorkbook.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row

' Pull out relevant data from master spreadsheet where column F = "John"
With ThisWorkbook.Sheets("Master")
    .AutoFilterMode = False
    .Range("A2:Z2").AutoFilter Field:=6, Criteria1:="John"
    ' Copy and paste NBK
    .Range("A2:A" & LastRow).Copy
        ActiveWorkbook.Sheets("Sheet1").Range ("A11")
End With

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationAutomatic

End Sub
Was it helpful?

Solution

Figurd it out!

With ThisWorkbook.Sheets("Master")
    .AutoFilterMode = False
    .Range("A2:Z2").AutoFilter Field:=6, Criteria1:="John"
    .Range("A2:Z2").AutoFilter Field:=8, Criteria1:="Desktop"
    ' Copy and paste NBK, first name, last name
    .Range("A3:C" & LastRow).Copy
        ActiveWorkbook.Sheets("Sheet1").Range("A" & NextHeaderRow + 5).PasteSpecial xlPasteValues
    ' Copy and paste email
    .Range("E3:E" & LastRow).Copy
        ActiveWorkbook.Sheets("Sheet1").Range("D" & NextHeaderRow + 5).PasteSpecial xlPasteValues
End With
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top