Conditional Copying of Cells to new Excel Sheet

rajeevmakayiram

New Member
Joined
Sep 23, 2011
Messages
3
Hi All,

I would like to do conditional copying of cells from one sheet to other sheet.

Eg: A Sheet Contains a list server inventory. In J column I have "Production" ,"Test", "Blanks", "DR". I just wanna filter only the Test and Blanks cells and copy the corresponding value from B column. Eg: If J2 is "Test" it should copy and paste the value of B2 to another excel file. Similary if J3 is Blank it should copy and paste the value of B3 to the next row. The Test and Blank corresponding vlues shoulb appended in the new excel. This should run as a loop for the entire J Column.

Please assist.

Regards
Rajeev
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I generally try to avoid loops when possible. This option will allow you to capture the data as you describe all at once. You will need to update the sheet names and destination range since that wasn't provided.

Code:
Sub Visible_Cells()
Dim LR As Long
Range("B2").End(xlDown).Select
LR = ActiveCell.Row
 
    Sheets("Sheet1").Activate
    Range("A:J").Select
    Selection.AutoFilter Field:=10, Criteria1:="=BLANK", Operator:=xlOr, _
        Criteria2:="=TEST"
    Range("B2:B" & LR).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets("Sheet2").Select
    ActiveSheet.Paste
 
End Sub
 
Upvote 0
Thanks a lot for your help Sir !!

I have modified my script like this (as per my req.)

Set objExcel = CreateObject("Excel.Application")
Sub Visible_Cells()
Dim LR
Range("G2").End(xlDown).Select
LR = ActiveCell.Row

Sheets("C:\Scripts\Data\Sheet1").Activate
Range("A:S").Select
Selection.AutoFilter Field:=18, Criteria1:="=Test", Operator:=xlOr
Range("F2:F" & LR).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("C:\Scripts\Data\Sheet2.xls").Select
ActiveSheet.Paste

End Sub


But I get Runtime Erro

Line No: 10
Char 28
Expected Statement Error.

Please advise.

Regards
Rajeev
 
Upvote 0
I see in your code that it appears you are only filtering for TEST, and not both TEST or BLANK. If this is the case then you do not need the:
Code:
", Operator:=xlOr"
a the end of your line:
Code:
Selection.AutoFilter Field:=18, Criteria1:="=Test

If you want to filter for two criteria then keep the OR section as below:
Code:
    Selection.AutoFilter Field:=10, Criteria1:="=BLANK", Operator:=xlOr, _
        Criteria2:="=TEST"
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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