I have some SQL results which I want to copy based to cell value. I found macro on the internet, but problem is that I don't want to copy/paste entire row.
Only columns from A to E where in column A is word which I am looking for. At the moment macro working fine, but doing entire row which is not really good my needs.
Can you help please to amend macro to copy paste just A-E columns, not entire row
Only columns from A to E where in column A is word which I am looking for. At the moment macro working fine, but doing entire row which is not really good my needs.
Can you help please to amend macro to copy paste just A-E columns, not entire row
Order Type | Order Date | Hour | Orders | Units |
Good | 14/12/2022 | 00:00 - 01:00 | 189 | 510 |
Good | 14/12/2022 | 01:00 - 02:00 | 102 | 282 |
Good | 14/12/2022 | 02:00 - 03:00 | 53 | 138 |
Elite | 14/12/2022 | 00:00 - 01:00 | 258 | 888 |
Elite | 14/12/2022 | 01:00 - 02:00 | 109 | 390 |
Elite | 14/12/2022 | 02:00 - 03:00 | 58 | 229 |
Together | 14/12/2022 | 00:00 - 01:00 | 447 | 1398 |
Together | 14/12/2022 | 01:00 - 02:00 | 211 | 672 |
Together | 14/12/2022 | 02:00 - 03:00 | 111 | 367 |
VBA Code:
Sub CopyRow2()
'Declare variables
Dim sheetNo1 As Worksheet
Dim sheetNo2 As Worksheet
Dim sheetNo3 As Worksheet
Dim sheetNo4 As Worksheet
Dim FinalRow As Long
Dim Cell As Range
'Set variables
Set sheetNo1 = Sheets("SQL")
Set sheetNo2 = Sheets("Good")
Set sheetNo3 = Sheets("Elite")
Set sheetNo4 = Sheets("Together")
'Type a command to select the entire row
'Selection.EntireRow.Select
Range("A2:E75").Select
' Define destination sheets to move row
FinalRow1 = sheetNo1.Range("A" & sheetNo1.Rows.Count).End(xlUp).Row
FinalRow2 = sheetNo2.Range("A" & sheetNo2.Rows.Count).End(xlUp).Row
FinalRow3 = sheetNo3.Range("A" & sheetNo3.Rows.Count).End(xlUp).Row
FinalRow4 = sheetNo4.Range("A" & sheetNo4.Rows.Count).End(xlUp).Row
With sheetNo1
'Apply loop for column E until last cell with value
For Each Cell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
If Cell.Value = "Good" Then
.Rows(Cell.Row).Copy Destination:=sheetNo2.Rows(FinalRow2 + 1)
FinalRow2 = FinalRow2 + 1
'Apply condition to match the "Unsold" value
ElseIf Cell.Value = "Elite" Then
'Command to Copy and move to a destination Sheet "Unsold2"
.Rows(Cell.Row).Copy Destination:=sheetNo3.Rows(FinalRow3 + 1)
FinalRow3 = FinalRow3 + 1
ElseIf Cell.Value = "Together" Then
'Command to Copy and move to a destination Sheet "Unsold2"
.Rows(Cell.Row).Copy Destination:=sheetNo4.Rows(FinalRow4 + 1)
FinalRow4 = FinalRow4 + 1
End If
Next Cell
End With
End Sub
Last edited by a moderator: