Copy and paste to second Sheet (Conditional)

Raph85

New Member
Joined
Dec 20, 2015
Messages
26
Hi Excel experts,

I have a data that has a dept code on it on column A and I would like to copy only the data on dept code 1 to sheet 2.

Could you please kindly share your expertise on the VBA code for this method.

Thank you.

Raphael.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi Raphael

1) You do mean copy, not move (i.e. you don't want to delete and close rows up after copy)?
2) How do you plan to identify which department code to copy?
3) Is there only one row with the required department code (if so, I am thinking double click the code and it copies across)?
4) Only the department code and nothing else in the row?
5) Where do you want to put it (e.g. A2; column A, next blank row)?

It always helps if you able to post a sample (changed names for privacy).

To best way to post a sample is using an Excel html add-in, which allows those who answer to copy your values and paste into Excel (saves us time). There are a few choices listed under 'Posting Aids' in the following thread. I use MrExcel HTML Maker, which is easy to use.

http://www.mrexcel.com/forum/board-announcements/127080-guidelines-forum-use.html
 
Upvote 0
Hi Steve,

Thank you for the reply. I was looking for the way to post the data so it's easier to be understood. :) please find the answer to your questions as per below:

1) You do mean copy, not move (i.e. you don't want to delete and close rows up after copy)?
I would like to copy all the data under Dept code 1 to sheet 2, then I will clear all the data on Sheet 1. basically this is a
finance statistic data which I have to do it daily.


2) How do you plan to identify which department code to copy?
there is column A on the data which show the dept number.

3) Is there only one row with the required department code (if so, I am thinking double click the code and it copies across)?
there is multiple row, depending on the number of transactions happening on the previous day.

4) Only the department code and nothing else in the row?
I would like to copy the entire row under dept code 1.

5) Where do you want to put it (e.g. A2; column A, next blank row)?
I am thinking of putting the heading on row 1 in sheet 2 so that it could be copied on the next blank row. I think that would
be the easiest way.



Excel 2012
ABCDEFG
1
2DeptAccountCurr/StatAccount DescAmountAccount TypeStatus
3
412100RM BAR48RevenueOpen
512100AUDBAR-11478.62RevenueOpen
612160RM CONS7RevenueOpen
712160AUDCONS-1775.47RevenueOpen
812300RM CORP32RevenueOpen
912300AUDCORP-6132.34RevenueOpen
1012350RM LCL15RevenueOpen
1112350AUDLCL-3320.17RevenueOpen
1212500RM MK47RevenueOpen
1312500AUDMK-9653.68RevenueOpen
1412600RM DIS23RevenueOpen
1512600AUDDIS-3926.13RevenueOpen
16197010AUDMTG-545.45RevenueOpen
17197510AUDAV0RevenueOpen
18198990AUDFA90.91ExpenseOpen
19257963AUDIM-2.9RevenueOpen
20257980AUDSUN0RevenueOpen
21317931AUDGV-216.23RevenueOpen
22408130AUDLR0RevenueOpen
23408140COV DR13RevenueOpen
24408140AUDDR-511.8RevenueOpen
25408990AUDFA-0.24ExpenseOpen
26409110AUDBR-179.54RevenueOpen
279996261AUDST-4037.26LiabilityOpen
Net View
 
Last edited:
Upvote 0
Try this script:

Code:
Sub FilterMini()
Dim Lastrow As Long
Lastrow = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
    With Worksheets("Sheet1").Range("A1").CurrentRegion
        .AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
        .SpecialCells(xlCellTypeVisible).Copy Worksheets("Sheet2").Range("A" & Lastrow)
    End With
ActiveSheet.AutoFilterMode = False

End Sub
 
Upvote 0
If you want to delete those rows on sheet One after copying them to sheet 2 then use the below script.
This script will copy the rows to sheet two and then delete them on sheet one.
Code:
Sub FilterMini()
'Mod 2
Dim Lastrow As Long
Lastrow = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
    With Worksheets("Sheet1").Range("A1").CurrentRegion
        .AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
        .SpecialCells(xlCellTypeVisible).Copy Worksheets("Sheet2").Range("A" & Lastrow)
        .SpecialCells(xlCellTypeVisible).Delete
    
    End With
ActiveSheet.AutoFilterMode = False

End Sub
 
Upvote 0
The script is looking for the value '1" in column "A" on Sheet(1).
And will copy that row to sheet 2.
 
Upvote 0
The reason why my script did not work for you was because you never mentioned in your original post that your data began in row (4). Your original post was very lacking in detail.

It's very unusual for someone to put their header in row (2) and then start their data in row (4). You can always get better and quicker help if you give all these details in the beginning. Now 3 hrs. later you did post a copy of your sheet and now we can see that.

Try the below script:
Code:
Sub FilterMini()
'Mod 2
Dim Lastrow As Long
Lastrow = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
    With Worksheets("Sheet1").Range("A4").CurrentRegion
        .AutoFilter Field:=1, Criteria1:="1", Operator:=xlFilterValues
        .SpecialCells(xlCellTypeVisible).Copy Worksheets("Sheet2").Range("A" & Lastrow)
    End With
ActiveSheet.AutoFilterMode = False

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,693
Messages
6,126,242
Members
449,304
Latest member
hagia_sofia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top