Adding Numerical Value to Range of Cells Based on Value in Adjacent Range of Cells

seattletimebandit

Board Regular
Joined
Apr 11, 2013
Messages
69
Hello All,

I've been trying to find a way to search down a single column and based on the text value in a cell, enter a numerical value in the adjacent cell to the left. I want to be able to assign the numbers to allow me to sort the text columns in a particular order, by using the numbers in the adjacent column. I'm thinking a CASE SELECT scenario.

Code:
Select Case score
    Case "Benzene"
        result = "1"
    Case "Ethylbenzene"
        result = "2"
    Case "Toluene"
        result = "3"
    Case "m, p-Xylene"
        result = "4"
    Case "o-Xylene"
        result = "5"
    Case "Gasoline"
        result = "6"
    'etc.
End Select

Here are the tables before and after adding the sort number, and the table after sorting the data:


Before Adding Sort NumberAfter Adding Sort NumberAfter Sorted
Sort_OrderListSort_OrderListSort_OrderList
Benzene1Benzene1Benzene
Ethylbenzene3Ethylbenzene1Benzene
Gasoline6Gasoline2Toluene
m, p-Xylene4m, p-Xylene2Toluene
o-Xylene5o-Xylene3Ethylbenzene
Toluene2Toluene3Ethylbenzene
Gasoline Range Organics7Gasoline Range Organics4m, p-Xylene
#2 Diesel9#2 Diesel4m, p-Xylene
Diesel Range Organics8Diesel Range Organics5o-Xylene
Lube Oil10Lube Oil5o-Xylene
Benzene1Benzene6Gasoline
Ethylbenzene3Ethylbenzene6Gasoline
Gasoline6Gasoline7Gasoline Range Organics
m, p-Xylene4m, p-Xylene7Gasoline Range Organics
o-Xylene5o-Xylene8Diesel Range Organics
Toluene2Toluene8Diesel Range Organics
Gasoline Range Organics7Gasoline Range Organics9#2 Diesel
#2 Diesel9#2 Diesel9#2 Diesel
Diesel Range Organics8Diesel Range Organics10Lube Oil
Lube Oil10Lube Oil10Lube Oil

<tbody>
</tbody>

Thanks for your help in advance!

stb
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Assuming your Sort_Order is in column A, List is in column B and data starts in row 2, try the following macro. You can add the additional items to the Select Case loop.
Code:
Sub AddNumVal()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range, foundRng As Range
    Dim sAddr As String
    For Each rng In Range("B2:B" & LastRow)
        Set foundRng = Range("B:B").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
        If Not foundRng Is Nothing Then
            sAddr = foundRng.Address
            Do
                Select Case foundRng.Value
                    Case "Benzene"
                        foundRng.Offset(0, -1) = 1
                    Case "Ethylbenzene"
                        foundRng.Offset(0, -1) = 2
                    Case "Toluene"
                        foundRng.Offset(0, -1) = 3
                    Case "m, p-Xylene"
                        foundRng.Offset(0, -1) = 4
                    Case "o-Xylene"
                        foundRng.Offset(0, -1) = 5
                    Case "Gasoline"
                        foundRng.Offset(0, -1) = 6
                End Select
                Set foundRng = Range("B:B").FindNext(foundRng)
            Loop While foundRng.Address <> sAddr
            sAddr = ""
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Or a simpler version of mumps code
Code:
Sub AddNumVal()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
    Dim rng As Range
    For Each rng In Range("B2:B" & LastRow)
      Select Case rng.Value
          Case "Benzene"
              rng.Offset(0, -1) = 1
          Case "Ethylbenzene"
              rng.Offset(0, -1) = 2
          Case "Toluene"
              rng.Offset(0, -1) = 3
          Case "m, p-Xylene"
              rng.Offset(0, -1) = 4
          Case "o-Xylene"
              rng.Offset(0, -1) = 5
          Case "Gasoline"
              rng.Offset(0, -1) = 6
      End Select
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@Fluff: I don't know why I made it over-complicated. Thanks for the input. :)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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