Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Further improvement to this IP address sorting code.

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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert street address to zip code SJC Excel Worksheet Functions 1 April 25th 08 08:50 PM
VB code to determine address of activeworkbook Kevin Excel Discussion (Misc queries) 2 January 5th 08 01:57 AM
How do I put the zip code in the address label? xsell Excel Discussion (Misc queries) 1 December 1st 05 03:34 PM
Sorting IP Address ggant Excel Discussion (Misc queries) 12 November 28th 05 05:53 PM
Sorting an Address List steve_g Excel Worksheet Functions 4 June 17th 05 06:36 PM


All times are GMT +1. The time now is 06:22 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"