L
lawrenceabrown
Hi: I made an improvement to this code whereas it will cut in an * if
any IP section is missing. It results like this:
..232.152.15 *.232.152.015
222..152.15 222.*.152.015
222.232..15 222.232.*.015
222.232.152. 222.232.152.*
At the same time, I made a slight stylistic improvement to the code to
replace generic i and n variables with meaningful names. i as a loop
counter went out of style back in, oh, 1980.
I am posting here only the improved routine...copy the rest of the
code from above posts. I hope you find this useful.
Larry Brown
Private Function FormatIPNum(IPNum As String, PadIt As Boolean) As
Variant
Dim Section_Num As Long
Dim IP_Sect_Val As Long
Dim Sections() As String
Dim Fmt As String
Const Dot As String = "."
Const WildCard As String = "*"
On Error GoTo BadNumber
Sections() = Split(IPNum, Dot)
If UBound(Sections()) <> 3 Then Err.Raise 9 'must have 4 sections
If PadIt Then Fmt = "000"
For Section_Num = 0 To 3
On Error Resume Next
IP_Sect_Val = CLng(Sections(Section_Num)) 'this will give an
error if it's not a number
If Err.Number = 0 Then
If IP_Sect_Val < 0 Or IP_Sect_Val > 255 Then
Err.Raise 9
ElseIf IP_Sect_Val < 100 Then
'only numbers < 100 need changing
Sections(Section_Num) = Format$(IP_Sect_Val, Fmt)
End If
Else
Sections(Section_Num) = "*"
End If
Next Section_Num
FormatIPNum = Join(Sections(), Dot)
Exit Function
BadNumber:
FormatIPNum = CVErr(xlErrValue)
Exit Function
End Function
any IP section is missing. It results like this:
..232.152.15 *.232.152.015
222..152.15 222.*.152.015
222.232..15 222.232.*.015
222.232.152. 222.232.152.*
At the same time, I made a slight stylistic improvement to the code to
replace generic i and n variables with meaningful names. i as a loop
counter went out of style back in, oh, 1980.
I am posting here only the improved routine...copy the rest of the
code from above posts. I hope you find this useful.
Larry Brown
Private Function FormatIPNum(IPNum As String, PadIt As Boolean) As
Variant
Dim Section_Num As Long
Dim IP_Sect_Val As Long
Dim Sections() As String
Dim Fmt As String
Const Dot As String = "."
Const WildCard As String = "*"
On Error GoTo BadNumber
Sections() = Split(IPNum, Dot)
If UBound(Sections()) <> 3 Then Err.Raise 9 'must have 4 sections
If PadIt Then Fmt = "000"
For Section_Num = 0 To 3
On Error Resume Next
IP_Sect_Val = CLng(Sections(Section_Num)) 'this will give an
error if it's not a number
If Err.Number = 0 Then
If IP_Sect_Val < 0 Or IP_Sect_Val > 255 Then
Err.Raise 9
ElseIf IP_Sect_Val < 100 Then
'only numbers < 100 need changing
Sections(Section_Num) = Format$(IP_Sect_Val, Fmt)
End If
Else
Sections(Section_Num) = "*"
End If
Next Section_Num
FormatIPNum = Join(Sections(), Dot)
Exit Function
BadNumber:
FormatIPNum = CVErr(xlErrValue)
Exit Function
End Function