Efficient sorting of data with VBA with 3 criteria

Darthrocket

New Member
Joined
Nov 12, 2016
Messages
3
Hi Mr. Excel.

I am in the process of automating some stuff at work, and therefore I would like to sort some data with VBA, based on 3 criteria.

I would like so sort some data like seen below:

7AEUf2J.png


The way I am doing it now is like the following code:

Code:
Range("G7").Select
    Selection.FormulaArray = "=INDEX($E$7:$E$" & LastRow & ",MATCH($G7&H$6&$G$6, $D$7:$D$" & LastRow & "&$B$7:$B$" & LastRow & "&$C$7:$C$" & LastRow & ",0))"
    Selection.AutoFill Destination:=Range("G7:F" & LastRow2)

However this is terribly inefficient, and takes forever to calculate.

Unfortunately my VBA skill are not good enough yet, to figure out a solution, so I hope that you can help me.

Thanks!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Since Sample place is the first sort, I don't see how you deal with the next Sample place.
 
Upvote 0
Maybe I should have mentioned it, but first I sort for unique batchnumbers:

Code:
Range("D7:D" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
        "G7"), Unique:=True

Then I use the following formula (I will insert the names to hopefully make it easier to read): {=INDEX(Values, MATCH(Batchnumber&Impurity&SamplePlace;Batchnumber_range&Impurity_range&SamplePlace_range;0))}

Then I copy this formula down.

For now I am only interested in sample place 1.
 
Upvote 0
For anyone interested, I came up with the following solution, that is somewhat quicker. Note the cellrefenrences may not align with the example I posted above.

Code:
Option Explicit

Sub FilterUniqueBatchnumber()
Dim LastRowFrom As Long


LastRowFrom = ThisWorkbook.Worksheets("Data").Range("C1").End(xlDown).Row


Range("C1:C" & LastRowFrom).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
        "L1"), Unique:=True
        
End Sub

Code:
Sub Sort(ImpurityColumn As Variant)

Dim i As Long
Dim j As Long
Dim LastRow As Long

LastRow = ThisWorkbook.Worksheets("Data").Range("C1").End(xlDown).Row

j = 2
For i = 2 To LastRow

    If Range("A" & i) = Range(ImpurityColumn & "1") And Range("E" & i) = Range("K2") And Range("C" & i) = Range("L" & j) Then
    
    Range(ImpurityColumn & j) = Range("B" & i)
    j = j + 1
    End If

Next i
End Sub

Code:
Sub RunSort()

FilterUniqueBatchnumber

Call Sort("M")
Call Sort("O")

End Sub



Still any improvements are welcome!
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,021
Members
449,060
Latest member
LinusJE

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