if multiple conditions are true, then copy the row to a new sheet

D

Don Doan

Hi all,
I have a "master" spreadsheet and 3 other spreadsheets "Month","Vic", "Day"
all in the same workbook.
how can i create a macro that go through the "master" sheet and look at
column
D and E (in the same row) to find certain conditions:
if column D has "MVInd" and column E has the letter "M", then copy that row
to the spreadsheet "Month"
if column D has "COD" and column E has the letter "V", then copy that row to
the spreadsheet "Vic"
if column D has "OPTD" and column E has the letter "D", then copy that row
the spreadsheet "Day"
the macro should keep doing that until there is a blank row in the "master"
sheet.

Thank you very much.
 
J

Joel

Sub test()

MonthRowCount = 1
VicRowCount = 1
DayRowCount = 1
RowCount = 1
With Sheets("Master")
Do While .Range("D" & RowCount) <> ""
If .Range("D" & RowCount) = "MVInd" And .Range("E" & RowCount) = "M"
Then
.Rows(RowCount).Copy _
Destination:=Sheets("Month").Rows(MonthRowCount)
MonthRowCount = MonthRowCount + 1
End If
If .Range("D" & RowCount) = "COD" And .Range("E" & RowCount) = "V" Then
.Rows(RowCount).Copy _
Destination:=Sheets("Vic").Rows(VicRowCount)
VicRowCount = VicRowCount + 1
End If
If .Range("D" & RowCount) = "OPTD" And .Range("E" & RowCount) = "D" Then
.Rows(RowCount).Copy _
Destination:=Sheets("Day").Rows(DayRowCount)
DayRowCount = DayRowCount + 1
End If

RowCount = RowCount + 1
Loop
End With


End Sub
 
D

Don Doan

thank you very much, it works perfectly.

Joel said:
Sub test()

MonthRowCount = 1
VicRowCount = 1
DayRowCount = 1
RowCount = 1
With Sheets("Master")
Do While .Range("D" & RowCount) <> ""
If .Range("D" & RowCount) = "MVInd" And .Range("E" & RowCount) = "M"
Then
.Rows(RowCount).Copy _
Destination:=Sheets("Month").Rows(MonthRowCount)
MonthRowCount = MonthRowCount + 1
End If
If .Range("D" & RowCount) = "COD" And .Range("E" & RowCount) = "V" Then
.Rows(RowCount).Copy _
Destination:=Sheets("Vic").Rows(VicRowCount)
VicRowCount = VicRowCount + 1
End If
If .Range("D" & RowCount) = "OPTD" And .Range("E" & RowCount) = "D" Then
.Rows(RowCount).Copy _
Destination:=Sheets("Day").Rows(DayRowCount)
DayRowCount = DayRowCount + 1
End If

RowCount = RowCount + 1
Loop
End With


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top