Thread: Range Vlookup
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel[_925_] joel[_925_] is offline
external usenet poster
 
Posts: 1
Default Range Vlookup


Are each of your areas unique or are there overlapping areas?

for eample
Area A : 190.0.0.0 to 199.255.255.255
Area B : 192.0.0.0 to 192.255.255.255

This code works for non-over lapping IP addresses. I would have to
modify the code to test for best match.

Sub SplitIP()

LookupIP = "192.170.30.30"

With Sheets("Sheet1")
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
For RowCount = 1 To LastRow
IP1 = Split(.Range("A" & RowCount), ".")
IP2 = Split(.Range("B" & RowCount), ".")
Area = .Range("C" & RowCount)
With Sheets("Sheet2")
For Index = 0 To 3
.Range("A" & RowCount).Offset(0, Index) = Val(IP1(Index))
.Range("E" & RowCount).Offset(0, Index) = Val(IP2(Index))
Next Index
.Range("I" & RowCount) = Area
End With
Next RowCount
End With

'sort IP address
With Sheets("Sheet2")
.Rows("1:" & LastRow).Sort _
Header:=xlNo, _
key1:=.Range("D1"), _
Order1:=xlAscending
.Rows("1:" & LastRow).Sort _
Header:=xlNo, _
key1:=.Range("A1"), _
Order1:=xlAscending, _
key2:=.Range("B1"), _
Order2:=xlAscending, _
key3:=.Range("C1"), _
Order3:=xlAscending

LookupIPArray = Split(LookupIP, ".")
Found = "Low"
RowCount = 1
Do While RowCount <= LastRow
For Index = 0 To 3
Field = Val(LookupIPArray(Index))
If .Range("A" & RowCount).Offset(0, Index) < Field Then
Exit For
End If

If Field < .Range("E" & RowCount).Offset(0, Index) Then
Found = "Match"
Area = .Range("I" & RowCount)
Exit For
End If

If Field .Range("E" & RowCount).Offset(0, Index) Then
Found = "High"
Exit For
End If

'required if IP exactly matches highest address in range
If Index = 3 Then
Found = "Match"
Area = .Range("I" & RowCount)
End If
Next Index
If Found = "High" Or _
Found = "Match" Then

Exit Do
End If
RowCount = RowCount + 1
Loop

If Found = "High" Then
MsgBox ("IP not found : " & LookupIP)
End If
If Found = "Match" Then
MsgBox ("Area : " & Area)
End If

End With

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?u=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=198408

http://www.thecodecage.com/forumz