VBA Query help copy paste

sowmyag

New Member
Joined
Apr 3, 2014
Messages
10
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 ?

Col1Col2Col3Col4
123​
xxxyyyB1
123​
xxxyyyC
123​
xxxyyyB2
145​
xxxyyyB3
145​
xxxyyyC

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
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
In your description you have columns 1, 2, 3 and 4.
But in your macro you have column AR, so I don't know what your column 1 is.
Adjust the columns in the macro, I put some notes so you can identify where you should make changes.

VBA Code:
Sub Inscopemacro()
  Dim sh1 As Worksheet
  Dim a As Variant
  Dim dic As Object
  Dim rng As Range
  Dim i As Long, lr As Long
 
  Set sh1 = Sheets("Dremio")      'source sheet
  Set dic = CreateObject("Scripting.Dictionary")
  lr = sh1.Range("AR" & Rows.Count).End(3).Row
  Set rng = sh1.Range("A" & lr + 1)
  a = sh1.Range("A1:AR" & lr).Value
 
  For i = 2 To UBound(a, 1)
    Select Case a(i, 44)          '44 is AR column
      Case "B1", "B2"
        dic(a(i, 1)) = Empty      'Change the 1 to the column number
    End Select
  Next
 
  For i = 1 To UBound(a, 1)
    If dic.exists(a(i, 1)) Then   'Change the 1 to the column number
      Set rng = Union(rng, sh1.Range("A" & i))
    End If
  Next
 
  With Sheets("Inscope")          'destination sheet
    .Cells.ClearContents
    rng.EntireRow.Copy .Range("A2")
  End With
End Sub
 
Upvote 0
Col1 is 37 . Thanks for the query but its copying all the rows.

As per the table -

I want only first three rows which has B1 and B2 in col 4 and copy all the records of Col1 (including Col4 C) but the query is copying first three rows and also the last row but i dont want the last row.

The group of Col1 rows should be copied only if the value in Col4 has B1 and B2

Hope i am making sense.

Col1Col2Col3Col4
123xxxyyyB1
123xxxyyyC
123xxxyyyB2
145xxxyyyB3
145xxxyyyC
 
Upvote 0
IF Col4 = "B1 or B2" then copy all the rows which has the same value in Col1 = 123
The group of Col1 rows should be copied only if the value in Col4 has B1 and B2
I am a little confused.
It is difficult to understand your need with just one example.
The macro must be prepared for all possible scenarios, or try to cover all of them.

So, in the following sample, which groups should be copied, and if you could put a brief description of why it should be copied.
For example:
Group 123, YES must copy because ...
Group 456, should NOT be copied because ...
Group 888, ... etc.

Dante Amor
ABCD
1COL1COL2COL3COL4
2123b2c2d2
3123b3c3B1
4123b4c4d4
5123b5c5B2
6456b6c6d6
7456b7c7d7
8456b8c8d8
9888b9c9B1
10888b10c10d10
11888b11c11d11
12999b12c12B2
13999b13c13d13
14999b14c14d14
15123b15c15d15
16222b16c16B2
17222b17c17B1
18222b18c18D18
19654b19c19D19
20654b20c20D20
Dremio
 
Upvote 0
See below - I have updated your example.
For example:
Group 123, YES must copy because it has Col 4 B1 or B2
Group 456, should NOT be copied because Col4 has no value B1 or B2
Group 888, . should be copied because it has B1 value in Col 4
Group 999, . should be copied because it has B2 value in Col 4
Group 222, . should be copied because it has B2 or B1 value in Col 4
Group 222, . should NOT be copied because it has no B2 or B1 value in Col 4

Dremio
ABCD
Dante Amor
1COL1COL2COL3COL4
2123b2c2d2
3123b3c3B1
4123b4c4d4
5123b5c5B2
6456b6c6d6
7456b7c7d7
8456b8c8d8
9888b9c9B1
10888b10c10d10
11888b11c11d11
12999b12c12B2
13999b13c13d13
14999b14c14d14
15123b15c15d15
16222b16c16B2
17222b17c17B1
18222b18c18D18
19654b19c19D19
20654b20c20D20
 
Upvote 0
See below - I have updated your example.
For example:
Group 123, YES must copy because it has Col 4 B1 or B2
Group 456, should NOT be copied because Col4 has no value B1 or B2
Group 888, . should be copied because it has B1 value in Col 4
Group 999, . should be copied because it has B2 value in Col 4
Group 222, . should be copied because it has B2 or B1 value in Col 4
Group 654, . should NOT be copied because it has no B2 or B1 value in Col 4

Dante Amor
ABCD
Dremio
1COL1COL2COL3COL4
2123b2c2d2
3123b3c3B1
4123b4c4d4
5123b5c5B2
6456b6c6d6
7456b7c7d7
8456b8c8d8
9888b9c9B1
10888b10c10d10
11888b11c11d11
12999b12c12B2
13999b13c13d13
14999b14c14d14
15123b15c15d15
16222b16c16B2
17222b17c17B1
18222b18c18D18
19654b19c19D19
20654b20c20D20
 
Upvote 0
Try this:

VBA Code:
Sub Inscopemacro()
  Dim sh1 As Worksheet
  Dim a As Variant
  Dim dic As Object
  Dim rng As Range
  Dim i As Long
  
  Set sh1 = Sheets("Dremio")      'source sheet
  Set dic = CreateObject("Scripting.Dictionary")
  a = sh1.Range("A1", sh1.Range("AR" & Rows.Count).End(3)).Value
  
  For i = 2 To UBound(a, 1)
    If a(i, 44) = "B1" Or a(i, 44) = "B2" Then dic(a(i, 37)) = Empty
  Next
  
  For i = 1 To UBound(a, 1)
    If dic.exists(a(i, 37)) Then
      If rng Is Nothing Then Set rng = sh1.Range("A" & i) Else Set rng = Union(rng, sh1.Range("A" & i))
    End If
  Next
  
  With Sheets("Inscope")          'destination sheet
    .Cells.ClearContents
    rng.EntireRow.Copy .Range("A2")
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,669
Messages
6,173,703
Members
452,528
Latest member
ThomasE

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