Leading zeros in IP address?

C

Charlotte E.

If I have a string containing a given IP address - it could be any IP
address, like:

1.222.33.004

What would be the smartest/shortest code, to make sure that all subnets have
leading seros, like:

001.222.033.004



TIA,
 
J

Joel

Sub test()
IP = "1.222.33.004"
IParray = Split(IP, ".")

For i = 0 To 3
If i = 0 Then
IP = Format(Val(IParray(i)), "#000")
Else
IP = IP & "." & Format(Val(IParray(i)), "#000")
End If
Next i

End Sub
 
J

Joel

IP = "1.222.33.004"
IParrray = split(IP,".")

for i = 0 to 3
if i = 0 then
IP = format(val(IPArray(i)),"#000")
else
IP = IP & "." &format(val(IPArray(i)),"#000")
end if
next i
 
C

Charlotte E.

Almost work :)

It work when I don't have 'Option Explicit' enabled!

But with 'Option Explicit' enabled I get an error in the line:

IParray = split(IP,".")

If I don't Dim the variable I obviously get an "Variable not defined', but
if I try to Dim the variable with

Dim IParray(3) as Variant

I get a 'Cannot assign to Array'.


How to get by this problem???


CE
 
G

Gary Keramidas

charlotte:
because there is a typo, IParrray = split(IP,"."). notice there are 3 r's.

Sub test()
Dim IP As String
Dim IParray As Variant
Dim i As Long
IP = "1.222.33.004"
IParray = Split(IP, ".")

For i = 0 To 3
If i = 0 Then
IP = Format(Val(IParray(i)), "#000")
Else
IP = IP & "." & Format(Val(IParray(i)), "#000")
End If
Next i
Debug.Print IP
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top