The below macro will allow me to find a name in the heading in sheet 1 and copy the entire column to sheet 2. Now I want to continue the code, but am facing a problem, which I will try explain.
Sub CopyColumnByTitle()
'Find "Name" in Row 1
With Sheets(1).Rows(1)
Set t = .Find("Name", lookat:=xlpart)
'If found, copy the column to Sheet 2, Column A
'If not found, present a message
If Not t Is Nothing Then
Columns(t.Column).EntireColumn.Copy _
Destination:=Sheets(2).Range("A1")
Else: MsgBox "Title Not Found"
End If
End With
End Sub
after all data is pasted in the sheet 2 as below ....
Sheet 2
Name Age Address Date of Birth
John 25 US 1-Sep-11
Hary 26 US 1-Sep-11
John 27 UK 1-Sep-11
Hary 28 US 2-Sep-11
King 29 UK 3-Sep-11
Peter 30 US 3-Sep-11
I need to set filters as shown below and copy the filtered data to sheet 3 as above code does:
- I need to set filter criteria on sheet 2 which helps me to see
Name
s that are equal to "John" or "Hary" and copy and paste the entire data into sheet 3. - I need to set another filter where
Name
is equal to "John" andDate of Birth
is equal to "1-Sep-11" (note the date should always be yesterday). Copy and paste the entire data into sheet 4. - On the third time, I need to set a filter where
Name
is equal to "King" and copy the and past the entire data into sheet 5.
Update
Thanks a lot John for your reply, the reply you gave is effective but I have already designed my code.
I am pasting a part of code as it is not possible to paste the entire code.
The code allows me to copy data from one workbook to another, but while copying data, I need to copy an entire column because there are some blank cells in it. So if I don't use .EntireColumn
, the macro does not copy cells after the blank cell. Also now, while pasting the data into the other work book, I need to paste it without the heading.
Windows("macro2.xlsm").Activate
Range(Range("M2"), Range("N2").End(xlDown)).EntireColumn.Select
Application.CutCopyMode = False
Selection.Copy
Windows("formula.xls").Activate
Range(Range("I2"), Range("J2").End(xlDown)).EntireColumn.Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False