I need to find a value in a table based on 3 factors... The value in Column A (Salesperson Name), the value in Column B (Group Name), and the value in Row 1 which is my header row (Category)
right now, I'm going through cell by cell and seeing if these values match and if they do i'm using the number and getting out of the loop - the code works but is slow
is there a more efficient way? It is taking quite a long time because the table is large and this process has to be done hundreds of times.
my current code (condensed as it contained a lot of things not pertinent to this subject :
Code:
Private Sub calculateComm()
Dim i As Integer
Dim comms, pcts As Worksheet
Dim sp, group, category As String
Dim tbl, cel As Range
Set comms = ActiveWorkbook.Worksheets("Month Invoices")
Set pcts = ActiveWorkbook.Worksheets("Commission Percents")
Set tbl = pcts.Cells(1, 1).CurrentRegion
i = 4
For i = 4 To comms.Cells(4, 1).CurrentRegion.Rows.Count + 2
sp = Trim(comms.Range("D" & i).Text)
group = Trim(comms.Range("E" & i).Text)
category = Trim(comms.Range("N" & i).Text)
For Each cel In tbl.Cells
If (pcts.Cells(cel.Row, 1) = sp And pcts.Cells(cel.Row, 2) = group And pcts.Cells(1, cel.Column) = category) Then
comms.Range("Y" & i) = cel.Value
Exit For
End If
Next cel
comms.Range("Z" & i).Value = comms.Range("Y" & i).Value * comms.Range("Q" & i).Value
Next i
End Sub