VBA Function for comparing a value from a cell with values from another table

IosifFlorin

New Member
Joined
Jul 1, 2015
Messages
18
Hello! I am trying to write a new function because only with formulas is too complicated and inefficient


  • i have 2 tables (names below)
  • in the TableProd, header Current Band i want to find out where is positioned in the Banding Table (table 2) the Price for a certain Product.
  • i am using the tables to make it easier for referencing

Important: the comparison of the prices have to start with the most aggressive band in table 2 (which is the one most to the right, not left).

In CurrentBand i have the some examples of answer

I want to extend the function, this is just and example but the names of the headers are with these specifics, but would be really helpful to get the blueprint which i could expand afterwards

TableProd
ProductCodePrice /UnitPrice /BoxCurrent Band
Prod11050 Jupiter
Prod225125 Saturn
Prod353265 Between Saturn and Jupiter
Prod4525 < Jupiter

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>


Banding Table
ProductCodeVenus /UnitMars /UnitSaturn /UnitJupiter /Unit
Prod110101010
Prod250252520
Prod3100756050
Prod450302010

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Iosif,
Thre is many ways to do thei, here is an idea
Create Function keyband with this code
Code:
Function keyband(k As String, p As Double, r As Range) As String
    Dim rcols, rrows, f As Integer
    rcols = r.Columns.Count
    rrows = r.Rows.Count
    ' Looks for the Key in columns 1 of r
    f = 0
    For i = 2 To rrows
       If (k = r.Cells(i, 1).Value) Then
          f = i
          Exit For
       End If
    Next i
    ' If it finds the key looks for the column now
    If (f > 1) Then
       For i = rcols To 2 Step -1
          ' equal value the first to the right
          If (p = r.Cells(f, i).Value) Then
             keyband = "= " + r.Cells(1, i).Value
             Exit For
          ElseIf (p < r.Cells(f, i).Value And i = rcols) Then
          ' less then the first one
             keyband = "< " + r.Cells(1, i).Value
             Exit For
          ElseIf (p > r.Cells(f, i).Value And i = 2) Then
          ' grater thethelast one
             keyband = "> " + r.Cells(1, i).Value
             Exit For
          ElseIf (p > r.Cells(f, i).Value And p < r.Cells(f, i - 1).Value And i > 2) Then
          ' between 2 values
             keyband = "Between " + r.Cells(1, i - 1).Value + " and " + r.Cells(1, i).Value
             Exit For
          Else
          ' Error no case was found, problably the table/range r is in a wrong order
             keyband = "Error"
          End If
       Next i
    Else
    ' Did not find the key
       keyband = "n/a"
    End If
End Function
Use the funcion in E4
Code:
=keyband(A4,B4,$A$11:$E$15)

Then get these results

OVyXfF6lzCaXOLJKTSVcZAgQg1MOcVX9l92j6gMSctY


Cheers
Sergio
 
Upvote 0
Thanks a lot Sergio, works marvelously! :)
i changed a bit to work how i wanted, but is exactly what i needed help with :)
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,528
Members
449,316
Latest member
sravya

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