Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35
Default Funciton to subtract and add a digit to an IP address format?

Hello,

Is there an excel funciton I can use to subtract a digit and add a digit for
an IP address format? For example: I have a known IP address of
172.24.137.149 in cell B2 and I would like to create a funtion in cell A2 to
subtract 1 digit which would result in 172.24.137.148 and another function in
cell C2 to add 1 digit which would result in 172.24.137.150.

Thanks,
Jaret

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Funciton to subtract and add a digit to an IP address format?

Use the following UDFs:

Function upOne(r As Range) As String
s = Split(r.Value, ".")
s(UBound(s)) = s(UBound(s)) + 1
upOne = Join(s, ".")
End Function


Function downOne(r As Range) As String
s = Split(r.Value, ".")
s(UBound(s)) = s(UBound(s)) - 1
downOne = Join(s, ".")
End Function


--
Gary''s Student - gsnu200786


"mcmilja" wrote:

Hello,

Is there an excel funciton I can use to subtract a digit and add a digit for
an IP address format? For example: I have a known IP address of
172.24.137.149 in cell B2 and I would like to create a funtion in cell A2 to
subtract 1 digit which would result in 172.24.137.148 and another function in
cell C2 to add 1 digit which would result in 172.24.137.150.

Thanks,
Jaret

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,572
Default Funciton to subtract and add a digit to an IP address format?

I don't know squat about coding Gary, but I don't believe, by looking at
your UDF, that you took into consideration that an octet can *only* go to
255.

So,
172.15.255.255
plus 1, would be
172.16.0.0

Subtraction would also encompass similar multi-faceted calculations.
--
Regards,

RD

---------------------------------------------------------------------------
Please keep all correspondence within the NewsGroup, so all may benefit !
---------------------------------------------------------------------------
"Gary''s Student" wrote in message
...
Use the following UDFs:

Function upOne(r As Range) As String
s = Split(r.Value, ".")
s(UBound(s)) = s(UBound(s)) + 1
upOne = Join(s, ".")
End Function


Function downOne(r As Range) As String
s = Split(r.Value, ".")
s(UBound(s)) = s(UBound(s)) - 1
downOne = Join(s, ".")
End Function


--
Gary''s Student - gsnu200786


"mcmilja" wrote:

Hello,

Is there an excel funciton I can use to subtract a digit and add a digit

for
an IP address format? For example: I have a known IP address of
172.24.137.149 in cell B2 and I would like to create a funtion in cell

A2 to
subtract 1 digit which would result in 172.24.137.148 and another

function in
cell C2 to add 1 digit which would result in 172.24.137.150.

Thanks,
Jaret


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Funciton to subtract and add a digit to an IP address format?

On Wed, 14 May 2008 08:07:02 -0700, mcmilja
wrote:

Hello,

Is there an excel funciton I can use to subtract a digit and add a digit for
an IP address format? For example: I have a known IP address of
172.24.137.149 in cell B2 and I would like to create a funtion in cell A2 to
subtract 1 digit which would result in 172.24.137.148 and another function in
cell C2 to add 1 digit which would result in 172.24.137.150.

Thanks,
Jaret


You could try this UDF which will add (or subtract if you enter a negative
number for the second argument) any value from your IP address:

It can probably be simplified but it's late.

To use this, enter the function =AddIP(IP, Add) in some cell where IP is, or
refers to a cell reference containing, an IP address; and Add is the amount you
want to add or subtract.

A2: =AddIP(B2,-1)
C2: =AddIP(B2,1)

To enter this, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

The routine does not check that your original IP address is what is normally
considered valid; nor does it convert them.

================
Option Explicit
Function AddIP(IP As String, Add As Integer) As Variant
Dim aIP As Variant
Dim dWord As Double
Dim iIP As Variant
Dim i As Long

aIP = Split(IP, ".")
'convert to dWord and Add
For i = 0 To 3
dWord = dWord + aIP(i) * 256 ^ (3 - i)
Next i
dWord = dWord + Add

If dWord = 256 ^ 4 Then
AddIP = CVErr(xlErrNum)
Exit Function
End If

aIP(0) = Int(dWord / 256 ^ 3)
aIP(1) = Int((dWord - aIP(0) * 256 ^ 3) / 256 ^ 2)
aIP(2) = Int((dWord - aIP(0) * 256 ^ 3 - aIP(1) * 256 ^ 2) / 256)
aIP(3) = dWord - aIP(0) * 256 ^ 3 - aIP(1) * 256 ^ 2 - aIP(2) * 256

AddIP = Join(aIP, ".")
End Function
============================
--ron
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Funciton to subtract and add a digit to an IP address format?

On Thu, 15 May 2008 00:17:09 -0400, Ron Rosenfeld
wrote:

On Wed, 14 May 2008 08:07:02 -0700, mcmilja
wrote:

Hello,

Is there an excel funciton I can use to subtract a digit and add a digit for
an IP address format? For example: I have a known IP address of
172.24.137.149 in cell B2 and I would like to create a funtion in cell A2 to
subtract 1 digit which would result in 172.24.137.148 and another function in
cell C2 to add 1 digit which would result in 172.24.137.150.

Thanks,
Jaret


You could try this UDF which will add (or subtract if you enter a negative
number for the second argument) any value from your IP address:

It can probably be simplified but it's late.

To use this, enter the function =AddIP(IP, Add) in some cell where IP is, or
refers to a cell reference containing, an IP address; and Add is the amount you
want to add or subtract.

A2: =AddIP(B2,-1)
C2: =AddIP(B2,1)

To enter this, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.


This routine is a little simpler, and also checks that your octets are not
greater than 255:

===================================
Option Explicit
Function AddIP(IP As String, Add As Integer) As Variant
Dim aIP As Variant
Dim dWord As Double, dTemp As Double
Dim i As Long

aIP = Split(IP, ".")
'convert to dWord and Add
For i = 0 To 3
If aIP(i) 255 Then GoTo ErrExit
dWord = dWord + aIP(i) * 256 ^ (3 - i)
Next i
dWord = dWord + Add

If dWord = 256 ^ 4 Then GoTo ErrExit

For i = 0 To 3
If i 0 Then
dWord = dWord - aIP(i - 1) * 256 ^ (4 - i)
End If
aIP(i) = Int(dWord / 256 ^ (3 - i))
Next i

AddIP = Join(aIP, ".")
Exit Function
ErrExit: AddIP = CVErr(xlErrNum)
End Function
=============================
--ron


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35
Default Funciton to subtract and add a digit to an IP address format?

Hello Ron,

I followed your procedure below but it did not work for me. Any thoughts?

Thanks,
Jaret

"Ron Rosenfeld" wrote:

On Thu, 15 May 2008 00:17:09 -0400, Ron Rosenfeld
wrote:

On Wed, 14 May 2008 08:07:02 -0700, mcmilja
wrote:

Hello,

Is there an excel funciton I can use to subtract a digit and add a digit for
an IP address format? For example: I have a known IP address of
172.24.137.149 in cell B2 and I would like to create a funtion in cell A2 to
subtract 1 digit which would result in 172.24.137.148 and another function in
cell C2 to add 1 digit which would result in 172.24.137.150.

Thanks,
Jaret


You could try this UDF which will add (or subtract if you enter a negative
number for the second argument) any value from your IP address:

It can probably be simplified but it's late.

To use this, enter the function =AddIP(IP, Add) in some cell where IP is, or
refers to a cell reference containing, an IP address; and Add is the amount you
want to add or subtract.

A2: =AddIP(B2,-1)
C2: =AddIP(B2,1)

To enter this, <alt-F11 opens the VB Editor. Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.


This routine is a little simpler, and also checks that your octets are not
greater than 255:

===================================
Option Explicit
Function AddIP(IP As String, Add As Integer) As Variant
Dim aIP As Variant
Dim dWord As Double, dTemp As Double
Dim i As Long

aIP = Split(IP, ".")
'convert to dWord and Add
For i = 0 To 3
If aIP(i) 255 Then GoTo ErrExit
dWord = dWord + aIP(i) * 256 ^ (3 - i)
Next i
dWord = dWord + Add

If dWord = 256 ^ 4 Then GoTo ErrExit

For i = 0 To 3
If i 0 Then
dWord = dWord - aIP(i - 1) * 256 ^ (4 - i)
End If
aIP(i) = Int(dWord / 256 ^ (3 - i))
Next i

AddIP = Join(aIP, ".")
Exit Function
ErrExit: AddIP = CVErr(xlErrNum)
End Function
=============================
--ron

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Funciton to subtract and add a digit to an IP address format?

On Mon, 16 Jun 2008 14:03:05 -0700, mcmilja
wrote:

Hello Ron,

I followed your procedure below but it did not work for me. Any thoughts?


I have plenty of thoughts :-)

What does "did not work" mean? And what did you do?

If *I* follow the procedure, it works fine.
--ron
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
I have a 38 digit # and I want to subtract 27 thru 32 from 33 thru Joe Twelve Pack Excel Worksheet Functions 2 February 15th 08 09:49 PM
Format to Round to Tens Digit Andy Excel Discussion (Misc queries) 4 May 30th 07 02:46 AM
Format 2 digit year to 4 digit RealGomer Excel Discussion (Misc queries) 5 December 14th 06 01:45 PM
divide a total, subtract one then format as a percentage [email protected] Excel Worksheet Functions 4 November 27th 06 08:14 PM
How do i change the format of a 2 digit date BB Setting up and Configuration of Excel 1 September 17th 06 09:31 PM


All times are GMT +1. The time now is 06:08 AM.

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

About Us

"It's about Microsoft Excel"