Check condition which involves matching filtered tables and copying it in new sheet

amrita17170909

Board Regular
Joined
Dec 11, 2019
Messages
74
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
I have a filtered table named "Table 3".

I have to match filtered values from column A of "Table 3" to column A of "Table 2" and if the condition is met copy the entire row to a new sheet named "Table 4"

I have tried the below code without any success
VBA Code:
Sub Copy_data_to_report()

Application.ScreenUpdating = False

lastRow = Worksheets("Table 3").Cells(Rows.Count, 1).End(xlUp).Row

nextRow = 2
      
        For thisRow = 1 To lastRow
      
           If Worksheets("Table 3").Cells(thisRow, 1).Value = Worksheets("Table 2").Cells(thisRow, 1).Value Then
         
               Worksheets("Table 2").Rows(thisRow).Copy

               Worksheets("Table 4").Activate

               B = Worksheets("Table 4").Cells(Rows.Count, 1).End(xlUp).Row

               Worksheets("Table 4").Cells(B + 1, 1).Select

               Worksheets("Table 4").Paste
             
               nextRow = nextRow + 1
             
            End If
                  
   Next

Application.CutCopyMode = False

End Sub
 
Last edited by a moderator:
Try something like this

the "table 3" sheet is filtered
Book1
ABC
1
2
3
4A1b1c1
5A2b2c2
6A3b3c3
9A6b6c6
12A9b9c9
Table 3


the "table 4" sheet is NOT filtered
Book1
ABC
5A5b5c5
6A15b6c6
7A3b7c7
8A8b8c8
9A9b9c9
10A10b10c10
11A11b11c11
12A12b12c12
Table 4


The macro looks for the values in column A of the "table 3" sheet within column A of the "table 4" sheet, if the value exists, then copy the row from the "table3" to the "table4".
In my example the values A3 and A9 are in the "table3" and also in the "table4", the result is in the "table4":
Book1
ABC
5A5b5c5
6A15b6c6
7A3b7c7
8A8b8c8
9A9b9c9
10A10b10c10
11A11b11c11
12A12b12c12
13A3b3c3
14A9b9c9
Table 4
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,

Thanks so much for your help.

I hope this explains the problem a bit more.

I have the below Table 3 which has filtered values . This table has been filtered on a value column and only shows rows of data above a certain threshold.I have to use UserID to extract data from Table 2.

For e.g A1 under UserID has 2 rows under Table 3 whereas Table 2 has 3 rows and hence the final output (Table 4) should have 3 rows.

Table 3
User IDProgramValue
A1Abc
100000​
A1Xyz
20000​
A3BB
2500​
A3Abc
2500​
B1Xyz
300​
B3Abc
10​

Table 2 : Table 2 has list of all rows of data. The final output should look like Table 4

Table 2
User IDProgramValue
A1Abc
100000​
A1Xyz
20000​
A1AA
30000​
A2VV
20​
A3BB
-2500​
A3Abc
2500​
B1Xyz
300​
B1AA
20​
B2AA
-50​
B2Abc
10​
B3Abc
10​
B3Abc
-80​

Table 4: Final Output

Table 4
User IDProgramValue
A1Abc
100000​
A1Xyz
20000​
A1AA
30000​
A3BB
-2500​
A3Abc
2500​
B1Xyz
300​
B1AA
20​
B3Abc
10​
B3Abc
-80​

I hope this makes sense

Amrita
 
Upvote 0
I don't see the rows or the columns. I will assume that it is columns A, B and C and that the headings begin are in row 1 and the data begins in row 2 (in all sheets).

If the header is, for example, in row 4, then change in the macro where it says "A1 by "A4

VBA Code:
Sub Generate_table4_v3()
  Dim sh3 As Worksheet, sh2 As Worksheet, dic As Object
  Dim c As Range, a() As Variant, b() As Variant, i As Long, j As Long
 
  Set sh3 = Sheets("Table 3")
  Set sh2 = Sheets("Table 2")
  Set dic = CreateObject("Scripting.Dictionary")
 
  For Each c In sh3.Range("A1", sh3.Range("A" & Rows.Count).End(xlUp))
    If c.EntireRow.Hidden = False Then dic(c.Value) = Empty
  Next
 
  j = 1
  a = sh2.Range("A1:C" & sh2.Range("A" & Rows.Count).End(xlUp).Row).Value2
  ReDim b(1 To UBound(a), 1 To 3)
  For i = 1 To UBound(a)
    If dic.exists(a(i, 1)) Then
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
      b(j, 3) = a(i, 3)
      j = j + 1
    End If
  Next
  Sheets("Table 4").Range("A1").Resize(j - 1, 3).Value = b()
End Sub
 
Upvote 0
hi Dante Amor,

Thanks for the code but I should have clarified that the tables have columns from A to AB.

I am only bringing across columns A,B and C at this stage .

Are you able to provide an amended code to bring across all columns ?

Thanks,

Amrita
 
Upvote 0
Did you try the macro?
Is the ID always in column A?

Please Note
-----------------------
One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your data, its layout and the overall objective for it).
 
Upvote 0
Yes I tried the macro and it is working perfectly in terms of logic.

ID is always going to be in column A and the output picked up the correct ID's from Table 2 .

I just want to ensure that all the column values from A to AB are picked up in the final table i.e Table 4.

I hope that makes sense. I will ensure that I am as clear as possible going forward.

Apologies for the confusion
 
Upvote 0
Try this

VBA Code:
Sub Generate_table4_v4()
  Dim sh3 As Worksheet, sh2 As Worksheet, dic As Object, r As Range
  Dim c As Range, a() As Variant, i As Long, lr As Long
 
  Set sh3 = Sheets("Table 3")
  Set sh2 = Sheets("Table 2")
  Set dic = CreateObject("Scripting.Dictionary")
 
  For Each c In sh3.Range("A1", sh3.Range("A" & Rows.Count).End(xlUp))
    If c.EntireRow.Hidden = False Then dic(c.Value) = Empty
  Next
 
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row
  Set r = sh2.Range("A" & lr + 1)
  a = sh2.Range("A1:C" & lr).Value2
  For i = 1 To UBound(a)
    If dic.exists(a(i, 1)) Then Set r = Union(r, sh2.Range("A" & i))
  Next
  r.EntireRow.Copy Sheets("Table 4").Range("A1")
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,255
Messages
6,123,896
Members
449,132
Latest member
Rosie14

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