I am trying to copy the rows in a different sheet on the following conditions
IF Col4 = "B1 or B2" then copy all the rows which has the same value in Col1 = 123
The new worksheet should copy only first three rows.
I tried writing a query and i am able to copy only B1 and B2 but not the row which has C value in Col2 which has the same value 123 in Col1, please note i don't want to copy the last row which also has "C" value
Can someone please help ?
Please see my copy paste query below -
Sub Inscopemacro()
Application.ScreenUpdating = False
Dim sh1 As Worksheet, sh2 As Worksheet, b As Long, LastR1 As Long, LastR2 As Long
Dim rngCopy As Range, rng As Range, cel As Range
Dim strSearch1 As String, strSearch2 As String
Sheets("Inscope").Cells.Clear
strSearch1 = "B1"
strSearch2 = "B2"
Set sh1 = ActiveSheet 'Extract from Dremio worksheet
Set sh2 = ThisWorkbook.Sheets("Inscope")
LastR1 = sh1.Range("AR" & Rows.Count).End(xlUp).Row
LastR2 = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
Set rng = sh1.Range("AR2:AR" & LastR1)
For Each cel In rng.Cells
If cel.Value = strSearch1 Or cel.Value = strSearch2 Then
If rngCopy Is Nothing Then
Set rngCopy = sh1.Rows(cel.Row)
Else
Set rngCopy = Union(rngCopy, sh1.Rows(cel.Row))
End If
End If
Next
If Not rngCopy Is Nothing Then
rngCopy.Copy Destination:=sh2.Cells(LastR2, 1)
End If
Application.ScreenUpdating = True
End Sub
IF Col4 = "B1 or B2" then copy all the rows which has the same value in Col1 = 123
The new worksheet should copy only first three rows.
I tried writing a query and i am able to copy only B1 and B2 but not the row which has C value in Col2 which has the same value 123 in Col1, please note i don't want to copy the last row which also has "C" value
Can someone please help ?
Col1 | Col2 | Col3 | Col4 |
123 | xxx | yyy | B1 |
123 | xxx | yyy | C |
123 | xxx | yyy | B2 |
145 | xxx | yyy | B3 |
145 | xxx | yyy | C |
Please see my copy paste query below -
Sub Inscopemacro()
Application.ScreenUpdating = False
Dim sh1 As Worksheet, sh2 As Worksheet, b As Long, LastR1 As Long, LastR2 As Long
Dim rngCopy As Range, rng As Range, cel As Range
Dim strSearch1 As String, strSearch2 As String
Sheets("Inscope").Cells.Clear
strSearch1 = "B1"
strSearch2 = "B2"
Set sh1 = ActiveSheet 'Extract from Dremio worksheet
Set sh2 = ThisWorkbook.Sheets("Inscope")
LastR1 = sh1.Range("AR" & Rows.Count).End(xlUp).Row
LastR2 = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
Set rng = sh1.Range("AR2:AR" & LastR1)
For Each cel In rng.Cells
If cel.Value = strSearch1 Or cel.Value = strSearch2 Then
If rngCopy Is Nothing Then
Set rngCopy = sh1.Rows(cel.Row)
Else
Set rngCopy = Union(rngCopy, sh1.Rows(cel.Row))
End If
End If
Next
If Not rngCopy Is Nothing Then
rngCopy.Copy Destination:=sh2.Cells(LastR2, 1)
End If
Application.ScreenUpdating = True
End Sub