IP Address Range Calculator

bleauhaus

New Member
Joined
Oct 3, 2014
Messages
2
Im sure this has been posted but after reading over this forum and others i still cannot find a solution.

Given the first two columns of IP addresses i want to generate the third or even fourth Column


[TABLE="width: 327"]
<tbody>[TR]
[TD]StartIP[/TD]
[TD]EndIP[/TD]
[TD]Total IPs[/TD]
[TD]Total /24s[/TD]
[/TR]
[TR]
[TD]1.32.0.0[/TD]
[TD]1.32.127.255[/TD]
[TD="align: right"]32,766[/TD]
[TD="align: right"]128[/TD]
[/TR]
[TR]
[TD]2.15.0.0[/TD]
[TD]2.15.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
[TR]
[TD]31.206.0.0[/TD]
[TD]31.206.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
[TR]
[TD]39.112.0.0[/TD]
[TD]39.127.255.255[/TD]
[TD="align: right"]1,048,574[/TD]
[TD="align: right"]4096[/TD]
[/TR]
[TR]
[TD]42.16.0.0[/TD]
[TD]42.47.255.255[/TD]
[TD="align: right"]2,097,150[/TD]
[TD="align: right"]8190[/TD]
[/TR]
[TR]
[TD]42.186.0.0[/TD]
[TD]42.186.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
[TR]
[TD]42.195.0.0[/TD]
[TD]42.195.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
[TR]
[TD]49.51.0.0[/TD]
[TD]49.51.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
[TR]
[TD]59.110.0.0[/TD]
[TD]59.111.255.255[/TD]
[TD="align: right"]131,070[/TD]
[TD="align: right"]512[/TD]
[/TR]
[TR]
[TD]83.210.0.0[/TD]
[TD]83.210.31.255[/TD]
[TD="align: right"]8,190[/TD]
[TD="align: right"]32[/TD]
[/TR]
[TR]
[TD]85.208.0.0[/TD]
[TD]85.209.255.255[/TD]
[TD="align: right"]131,070[/TD]
[TD="align: right"]512[/TD]
[/TR]
[TR]
[TD]100.0.0.0[/TD]
[TD]100.41.255.255[/TD]
[TD="align: right"]2,752,512[/TD]
[TD="align: right"]10752[/TD]
[/TR]
[TR]
[TD]101.202.0.0[/TD]
[TD]101.202.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
[TR]
[TD]104.141.0.0[/TD]
[TD]104.141.255.255[/TD]
[TD="align: right"]65,534[/TD]
[TD="align: right"]256[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Off the top of my head
0.0.0.0 if we work right to left 0 = 0 to 255, and then next is also the same (this you know)

so

dim n, dim m

for n = 0 to 255
for m = 0 to 255

next m
next n

should walk up those two values

obvioulsy you need to constrain n and m respectively

generating two million IPs will outstrip the available row space of Excel as demonstrated with 100.0.0.0

or did I miss the point
 
Upvote 0
Hi,

Not all of us are IP wizards :) So what's the logic for getting the values in column C and D, once we know that, we should be able to get a formula to do the work

A UDF like this might work but it gives a value 2 greater than what you have


Excel 2010
ABCD
1StartIPEndIPTotal IPsTotal /24s
21.32.0.01.32.127.25532768128
32.15.0.02.15.255.25565536256
431.206.0.031.206.255.25565536256
539.112.0.039.127.255.25510485764096
642.16.0.042.47.255.25520971528192
742.186.0.042.186.255.25565536256
842.195.0.042.195.255.25565536256
949.51.0.049.51.255.25565536256
1059.110.0.059.111.255.255131072512
1183.210.0.083.210.31.255819232
1285.208.0.085.209.255.255131072512
13100.0.0.0100.41.255.255275251210752
14101.202.0.0101.202.255.25565536256
15104.141.0.0104.141.255.25565536256
Sheet1
Cell Formulas
RangeFormula
C2=TotalIPs(A2,B2)
D2=C2/256


Code:
Function TotalIPs(C1 As Range, C2 As Range)
   Dim diff(), I As Integer
   TotalIPs = 1
   ReDim Preserve diff(0 To UBound(Split(C1, ".")))
   For I = LBound(Split(C1, ".")) To UBound(Split(C1, "."))
        TotalIPs = TotalIPs * (Split(C2, ".")(I) - Split(C1, ".")(I) + 1)
   Next I
   'TotalIPs = TotalIPs - 2
End Function
 
Last edited:
Upvote 0
Thanks Guys

As Momentman pointed out let me paint a little better picture... Looking at a single range and quickly determining its size (manually) such as



ABCD

<colgroup><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #FAFAFA"]StartIP[/TD]
[TD="bgcolor: #FAFAFA"]EndIP[/TD]
[TD="bgcolor: #FAFAFA"]Total IPs[/TD]
[TD="bgcolor: #FAFAFA"]Total /24s[/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: #FAFAFA"]1.32.0.0[/TD]
[TD="bgcolor: #FAFAFA"]1.32.127.255[/TD]
[TD="align: right"]32768[/TD]
[TD="align: right"]128[/TD]

</tbody>

only takes a second but you have to compare each of the four octets (the number between the dots) then calculate in your head the sum difference. its not a big deal with a single range but when your dealing with a column of ranges as in the original table A an B - trying to determine the largest or smallest can be an irritating task. with a hundred ranges it quickly becomes near impossible. check out The TCP/IP Guide - IP Classless Addressing Block Sizes and "Classful" Network Equivalents
 
Upvote 0
As Momentman pointed out let me paint a little better picture...
Well, you really didn't do that as Momentman asked for the logic of getting the values in columns C and D. All you told us on that front was "calculate in your head".



Sorry, but it is up to you to understand what is required and explain it to us, not for us to go researching a topic we know nothing about. :eek:


Going on what Momentman did with his udf, here's a formula solution that produces the same results. Not the results you quoted, but you haven't told us how to get those numbers. Subtracting 2 would do it but I don't know the logic for why you would do that.


Excel Workbook
ABC
1StartIPEndIPTotal IPs
21.32.0.01.32.127.25532,768
32.15.0.02.15.255.25565,536
431.206.0.031.206.255.25565,536
539.112.0.039.127.255.2551,048,576
642.16.0.042.47.255.2552,097,152
742.186.0.042.186.255.25565,536
842.195.0.042.195.255.25565,536
949.51.0.049.51.255.25565,536
1059.110.0.059.111.255.255131,072
1183.210.0.083.210.31.2558,192
1285.208.0.085.209.255.255131,072
13100.0.0.0100.41.255.2552,752,512
14101.202.0.0101.202.255.25565,536
15104.141.0.0104.141.255.25565,536
Sheet2
 
Upvote 0
1. Did you see the solution i posted above?

2. Should the answer to the first example be 32768 or 32766? and whats your formula for getting the values in column D?
 
Upvote 0
Here is another formula attempt:

Excel Workbook
ABCD
1StartIPEndIPTotal IPsTotal /24s
21.32.0.01.32.127.25532768128
32.15.0.02.15.255.25565536256
431.206.0.031.206.255.25565536256
539.112.0.039.127.255.25510485764096
642.16.0.042.47.255.25520971528192
742.186.0.042.186.255.25565536256
842.195.0.042.195.255.25565536256
949.51.0.049.51.255.25565536256
1059.110.0.059.111.255.255131072512
1183.210.0.083.210.31.255819232
1285.208.0.085.209.255.255131072512
13100.0.0.0100.41.255.255275251210752
14101.202.0.0101.202.255.25565536256
15104.141.0.0104.141.255.25565536256
Sheet2
 
Upvote 0
Is that correct? Change the last octet in A2 to 255.
 
Upvote 0
Is that correct? Change the last octet in A2 to 255.

I don't know to be honest, mine returns 256 - is that not correct?

Excel Workbook
N
32.15.0.255
42.15.1.255
52.15.etc.255
62.15.etc.255
72.15.254.255
82.15.255.255
Sheet1
 
Upvote 0
An IP address is an unsigned 32-bit number. The four octets (each 0 to 255) are the four bytes, high to low. So ...

The difference between x.y.z.1 and x.y.z.2 is 256^0 = 1

The difference between x.y.1.z and x.y.2.z is 256^1 = 256

The difference between x.1.y.z and x.1.y.z is 256^2 = 65536

The difference between 1.x.y.z and 2.x.y.z is 256^3 = 16777216
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top