Copy-paste with condition

Boris7265

Board Regular
Joined
Apr 6, 2011
Messages
68
hELLO all of you.
I have a vey large spreadhseet OP3 with 20000-25000 rows in it and would like to copy paste rows in another woksheet of the same spreadsheet. The conditions are in column z and they are are:"SJJG:SERVICE - JJL, GARN" and "C02:Garnishment Filing Fee". I need to copy -paste rows if they meets thses conditons in column z .The below code that i have worked out , the problem that is only copy -paste only rows with :"SJJG:SERVICE - JJL, GARN" not second conditon.

Dim LDate As String
LFound = False
Dim c As Range
Dim c2 As Range
Dim Filename As String
Dim NewSheet As Worksheet
Filename = InputBox("Enter the file name")
Worksheets.Add().Name = Filename
Dim NewRow As Integer
NewRow = Worksheets(Filename).Range("Z1").Value + 1
Sheets("OP3").Activate
Dim strsearch As String, strsearch1 As String, lastline As Integer, tocopy As Integer
strsearch = "C02:Garnishment Filing Fee"
lastline = Range("Z65536").End(xlUp).Row
'NewRow = 1
' Copy cost for GARNISHEMNT to OP3GARN spreadsheet the same worksheet
For i = 1 To lastline
For Each c In Range("Z" & i & ":Z" & i)
If c.Text = strsearch Then
tocopy = 1
End If
Next c
If tocopy = 1 Then
Sheets(Filename).Range(NewRow & ":" & NewRow) = Range(i & ":" & i).Value
NewRow = NewRow + 1
End If
tocopy = 0
Next i
strsearch1 = "SJJG:SERVICE - JJL, GARN"
For i = 1 To lastline
For Each c In Range("Z" & i & ":Z" & i)
If c.Text = strsearch1 Then
tocopy = 1
End If
Next c
If tocopy = 1 Then
Sheets(Filename).Range(NewRow & ":" & NewRow) = Range(i & ":" & i).Value
NewRow = NewRow + 1
End If
tocopy = 0
Next i

Thank you for adivise .

Thnak you ,

Best Reagards,

BorisGomel
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Can't we use the autofilter feature here for column Z:

Sub CopySplRow()
With ActiveSheet
.AutoFilterMode = False
With Range("C1", Range("C" & Rows.Count).End(xlUp))
.AutoFilter 1, Criteria1:="=*do*", _
Operator:=xlOr, Criteria2:="=*done*"
On Error Resume Next
.Offset(0).SpecialCells(12).EntireRow.Copy
<Enter code here where to copy>
End With
End With
End Sub
 
Upvote 0
Replace C with Z and Criteria1 and Criteria 2 with your keywords

Can't we use the autofilter feature here for column Z:

Sub CopySplRow()
With ActiveSheet
.AutoFilterMode = False
With Range("C1", Range("C" & Rows.Count).End(xlUp))
.AutoFilter 1, Criteria1:="=*do*", _
Operator:=xlOr, Criteria2:="=*done*"
On Error Resume Next
.Offset(0).SpecialCells(12).EntireRow.Copy
<ENTER copy to where here code>
End With
End With
End Sub
 
Upvote 0
Thank you very much Parth13.

iT works very well .

A thousand thank you to you !!!

The Best Reagrds,

BorisGomel
 
Upvote 0

Forum statistics

Threads
1,224,547
Messages
6,179,436
Members
452,915
Latest member
hannnahheileen

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