How to take values in each cell in a column and use them to filter in another sheet

ventstoy

New Member
Joined
May 11, 2022
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone! I have a question here. I have two sheets, one of them contains the whole information (in my case a table with many rows and columns) and the second one is a macro where it shows how many times a value has repeated itself in the master sheet. The idea is to take the value shown in the cell in the second sheet (showing how many times a value is duplicated) and filter it in the master sheet to show only the cells containing this value. Then it goes to the second one in the duplicates sheet and so on until the end. The idea is that after the filter is done I run a separate macro on the results that are left. I can imagine that I need a for loop but not sure how to combine it with the filter. To understand better lets say we have in the duplicates sheet:

colA colB
abc 3
cvb 4
zxc 5

We take the first value (abc) here filter it in the master sheet, then goes to the second value (cvb) and so on until the end of the column (which would be unknown). What would the for loop syntax be? For the filter I use the following:

Sub Filter_CellValue1()

Sheets("inventory).Range("A1").AutoFilter field:=2, Criteria1:=Sheets("duplicates").Cells(2, 1).Value

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this:

VBA Code:
Sub TestFilter()
  Dim sh As Worksheet
  Dim c As Range
  
  Set sh = Sheets("inventory")
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  '
  For Each c In Sheets("duplicates").Range("A2", Sheets("duplicates").Range("A" & Rows.Count).End(3))
    sh.Range("A1").AutoFilter 2, c.Value
    sh.AutoFilter.Range.Select 'after the filter is done I run a separate macro on the results that are left.
  Next
  sh.ShowAllData
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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