Copy rows based on a condition/rule using VBA

sandor jeges

New Member
Joined
May 26, 2016
Messages
13
This is my current code can anyone help to make it so all rows from the performanceEQ sheet to copy to Output with the rule copy row over if there is a data in cell B2 down, then when there is no more data in the B Column, copy the same row but with the rule IF there is data in cell D2 down until there is no more data - Can anyone help!!!!???

Code:
Sub Stocktransfer()
Set i = Sheets("PerformanceEQ")
Set e = Sheets("Output")
Dim d
Dim j
d = 1
j = 2
 
'FOR THE SELLER
 
Do Until IsEmpty(i.Range("B" & j))
 
If i.Range("B" & j) = "VEAEWP" Then
d = d + 1
e.Rows(d).Value = i.Rows(j).Value
 
End If
j = j + 1
Loop
 
'FOR THE SELLER
 
Do Until IsEmpty(i.Range("D" & j))
 
If i.Range("D" & k) = "VERGRE" Then
d = d + 1
e.Rows(d).Value = i.Rows(d).Value
 
End If
j = j + 1
Loop
 
End Sub
 
Last edited by a moderator:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the Board!

It's generally a bad idea to duplicate data with copy/paste. You can easily keep the data together (think like a database), then use PivotTables to summarize based on your rules.
 
Upvote 0
Assuming you have "VEAEWP" in column "B"
And "VERGRE" in column "D"
I'm sure there is another way to do this but I do not know it.
Try this:
Code:
Sub Filter_Me()
Application.ScreenUpdating = False
Dim Lastrow As Long
Lastrow = Sheets("Output").Cells(Rows.Count, "A").End(xlUp).Row + 1
    
With ActiveSheet.Range(Cells(1, 2), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 4))
        
    .AutoFilter Field:=1, Criteria1:="VEAEWP"
    .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Output").Range("A" & Lastrow)
    .AutoFilter
    End With
With ActiveSheet.Range(Cells(1, 2), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 4))
Lastrow = Sheets("Output").Cells(Rows.Count, "A").End(xlUp).Row + 1
    .AutoFilter Field:=3, Criteria1:="VERGRE"
    .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Output").Range("A" & Lastrow)
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Smitty - I personally wanted to see if VBA would work - i do have a colleague saying SQL might work but i know very very little on SQL :(
Thanks for the response My Answer is This. ALthough i am getting a Debug issue on the line ".Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Output").Range("A" & Lastrow)"

Any thoughts on why?
 
Upvote 0
Do you have a sheet name:
"Output"

And do you have the values you mentioned in the columns "B" and "D"

All these values must be spelled exactly correct.
 
Upvote 0
Worksheet is called Output but I should of noted that the VEAEWP and VERGRE start in row 2 - i have headers which are B1 = Sell Portfolio and D1 = Buy Portfolio
 
Upvote 0
Sorry all good it works - thanks My answer is this!

Sorry another question how much harder would it be to rearrange and reduce the columns copied over to the output tab?
 
Last edited:
Upvote 0
Header row does not matter.

And you have:
"VEAEWP" in column "B"

and "VERGRE" in column "C"
 
Upvote 0
So now your saying it works?? What was wrong?

What columns do you want copied over?
 
Upvote 0
Thanks Smitty - I personally wanted to see if VBA would work - i do have a colleague saying SQL might work but i know very very little on SQL

I completely get it, VBA's awesome, and it's saved me thousands of hours over the years. My point was not about that though, but not duplicating data in the first place. If it exists in one place in your workbook, it doesn't need to be copied somewhere else.

As for SQL, that's a completely different story, and if you need a database or not.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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