Match comma delimited values in cell against list and match each value

Slawbawn

New Member
Joined
Mar 16, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I feel like this is probably one of those things that really simple and probably staring me in the face, but I'm just not catching onto it.

We have a spreadsheet which contains an IP column. This column could contain anything from 1+ IP address, column delimited, for each IP that the row applies to. We have another sheet which has a list of each individual IP, and their corresponding hostname, with one matching IP and host for each row. I'm trying to work out how to take the comma delimited IPs, match each one against individual IPs from the second sheet, and then add the corresponding hostnames, also comma delimited, into the hostname column. So, in essence, take multiple values from Sheet1:ColumnA, match against individual values in Sheet2:ColumnA and input the values from Sheet2:ColumnB to Sheet1:ColumnB.

Book1
B
3
Results

Book1
B
7
Hosts


Hopefully the two tables above demonstrate how the data is shown. I've included an example of what a complete one would look like in the first row on sheet 1.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Book1
AB
1IPsHostnames
21.1.1.1, 2.2.2.2, 3.3.3.3.3Host1, Host2, Host3
34.4.4.4, 5.5.5.5
43.3.3.3, 4.4.4.4, 1.1.1.1
Results

Book1
AB
1IPHostname
21.1.1.1Host1
32.2.2.2Host2
43.3.3.3Host3
54.4.4.4Host4
65.5.5.5Host5
Hosts


Just realised I did the tables completely wrong
 
Upvote 0
Welcome to the Board!

Just to see if I understand this correctly, you want to populate column B on Sheet1.
Though in your example you only did the first row, when complete, should it look like this?
IPs
Hostnames
1.1.1.1, 2.2.2.2, 3.3.3.3.3Host1, Host2, Host3
4.4.4.4, 5.5.5.5Host4, Host5
3.3.3.3, 4.4.4.4, 1.1.1.1Host3, Host4, Host1

Are you OK with a VBA solution?
 
Upvote 0
OK, I created my own user defined function (UDF) in VBA to do this.
The code looks like this:
VBA Code:
Function IPLookup(ips As String, rng As Range) As String
'   Looks up IP addresses and returns string of hosts
'   Input Variables:
'       - ips = string of IP addresses to look up
'       - rng = range of lookup table

    Dim ipArr() As String
    Dim i As Long
    Dim ip As String
    Dim host As String
    Dim temp As String
    
'   Split list of ips into an array
    ipArr = Split(ips, ",")
    
'   Loop through list of ips
    For i = LBound(ipArr) To UBound(ipArr)
'       Get particular ip address
        ip = Trim(ipArr(i))
'       Lookup ip address in lookup table
        On Error GoTo err_bad_ip
        host = Application.WorksheetFunction.VLookup(ip, rng, 2, 0)
'       Add host to string
        temp = temp & host & ", "
        On Error GoTo 0
    Next i
    
'   Return string
    If Len(temp) > 0 Then
        IPLookup = Left(temp, Len(temp) - 2)
    End If

    Exit Function
    
    
err_bad_ip:
'error handler to handle bad ips
    host = "BAD IP"
    Err.Clear
    Resume Next

End Function
Note that if there is an IP addres for which there is no match, it will return "BAD IP" for that one.

Then you would just use it like any other function in Excel.
So, if you wanted to look up the value in cell A2, you would enter a formula like this:
Excel Formula:
=iplookup(A2,Hosts!A$2:B$6)
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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