VBA Script Edits: Secondary Filter+Sort+Export

Zarfot

New Member
Joined
Sep 23, 2016
Messages
19
Geniuses,

I am trying to adapt a script I have to add a more complicated step in filtering, sorting, and looping through my data.

Range 1 = Column I2:I
Range 2 = Column J2:J

Basically, I need a way to rework the code to select the first unique value in Range 1, then select the first unique value in Range 2, and run a sort and export. THEN, start the process over, however select the first unique value in Range 1, and select the SECOND unique value in Range 2, and run a sort. This would continue until all unique values in Range 2 are exhausted, at which point I would want the script to move to the second unique value in Range 1. This would repeat until all values in both ranges are processed.

Here is the current code that only selects from Range 1:
Code:
Set Rng = ws3.Range("i2:i" & ws3.Cells(Rows.Count, "i").End(xlUp).Row)
Rng.Sort Key1:=ws3.Range("I1"), Order1:=xlAscending


This would need to be adapted to also include values from Range 2 somewhere where it mentions Range.Autofilter Field:=2. In this case, Range 1 is Field:=2, and Range 2 is Field:=3

Code:
lRow = 6 'set current last row for start of ws3 summary sheet
 
'loop to copy row 3 from ws1 to ws2
 
For Each cell In Rng
 
    'increment last row
    i = i + 1
 
    With ws1
        .ListObjects("Table3").Range.AutoFilter Field:=2, Criteria1:=cell.Value
        .Range("B3:E3").Copy
        ws2.Range("B" & lRow + i).PasteSpecial xlPasteValues
    End With


The main reason why is I want to export a table showing unique threat values covering a specific area. Right now, the code only exports 1 threat per area, not multiple.

Example of the Dataset:

InspectionAreaThreatConditionSq. Footage
Inspection_1300AGreenGood3000
Inspection_1300AAmberPoor2000
Inspection_1300AAmberPoor1000
Inspection_1101CRedDamaged5000
Inspection_1101CRedDamaged4000
Inspection_2400ARedDamaged300
Inspection_2400BRedDamaged100
Inspection_2400AGreenGood50000
Inspection_2400BAmberPoor750
Inspection_2221XYellowOkay20000
Inspection_3X7647GreenGood58000
Inspection_3X7647YellowOkay9000
Inspection_3X2243RedDamaged90
Inspection_3X2243GreenGood600
Inspection_3X2243GreenGood400

<tbody>
</tbody>


Example of what the script currently does (when filtered for Inspection_1 as desired):

AreaThreatConditionSq. Footage
101CRedDamaged9000
300AGreenGood6000

<tbody>
</tbody>


Example of what I want (when filtered for Inspection_1 as desired):

AreaThreatConditionSq. Footage
300AGreenGood3000
300AAmberPoor3000
101CRedDamaged9000

<tbody>
</tbody>

Link to workbook file:
http://s000.tinyupload.com/download.php?file_id=61404658697921899524&t=6140465869792189952468930


Full Code:

Code:
Option Explicit
 
Sub Loop1()
Dim cell As Range 'loop range
Dim Rng As Range 'range for unique values
 
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
 
Dim lRow As Long 'last row in Inspection sheet
Dim i As Integer 'counter
 
Set ws1 = Worksheets("Data")
Set ws2 = Worksheets("Inspection")
Set ws3 = Worksheets("NamedRange")
 
Application.ScreenUpdating = False
 
'reset autofilter
ws1.ListObjects("Table3").Range.AutoFilter
 
'autofilter on Inspection selected
ws1.ListObjects("Table3").Range.AutoFilter Field:=1, Criteria1:=ws2.Range("C3")
 
'copy Column B in Table3 to NamedRange!I1
ws1.Range("B6:B20").SpecialCells(xlVisible).Copy 'extend range when needed
ws3.Range("I1").PasteSpecial
 
'copy Column C in Table3 to NamedRange!J1
ws1.Range("C6:C20").SpecialCells(xlVisible).Copy 'extend range when needed
ws3.Range("J1").PasteSpecial
 
'Remove duplicates for unique values
ws3.Columns("I:I").RemoveDuplicates Columns:=1, Header:=xlYes
 
'Remove duplicates for unique values
ws3.Columns("J:J").RemoveDuplicates Columns:=1, Header:=xlYes
 
'set range for loop and sort
Set Rng = ws3.Range("i2:i" & ws3.Cells(Rows.Count, "i").End(xlUp).Row)
Rng.Sort Key1:=ws3.Range("I1"), Order1:=xlAscending
 
lRow = 6 'set current last row for start of ws3 summary sheet
 
'loop to copy row 3 from ws1 to ws2
 
For Each cell In Rng
 
    'increment last row
    i = i + 1
 
    With ws1
        .ListObjects("Table3").Range.AutoFilter Field:=2, Criteria1:=cell.Value
        .Range("B3:E3").Copy
        ws2.Range("B" & lRow + i).PasteSpecial xlPasteValues
    End With
 
Next
 
'goto ws2.Range
Application.Goto ws2.Range("B6")
 
Application.ScreenUpdating = True
 
End Sub

Thanks for the help!
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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