Solved

Excel formula to complete missing gaps of IP addresses range

Posted on 2011-09-26
14
641 Views
Last Modified: 2012-05-12
Hi,

I have an excel file which has 1 column, data starts in A2 cell (A1=heading)
lets say there's a range of:
191.192.192.0 - 191.192.195.254 in ascending order

I would like somehow to fill in the missing gaps in the correct position, for example:

191.192.192.1
191.192.192.2
191.192.192.3
<automatically insert 191.192.192.4>
191.192.192.5
191.192.194.50
191.192.194.51
<automatically insert 191.192.194.52>
<automatically insert 191.192.194.53>
191.192.194.54

I would like to get a complete solution / VBA script if possible.

Thanks a bunch.

P.S.
Excel 2010
0
Comment
Question by:iNc0g
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 36598202
You could put 191.192.192.1 on cell A2 and 191.192.192.2 on cell A3 and then drag below to autofill the rest of the cells creating a list of all of the IP's...
0
 
LVL 10

Expert Comment

by:ukerandi
ID: 36598209
see the attached file click on command button it will work
IpAddress.xls
0
 
LVL 10

Expert Comment

by:ukerandi
ID: 36598215
Dim count As Long
Dim newNumber As String
Dim last2() As String
Dim Splitnumber As Long

For count = 1 To 100




If Cells(count, 1) = "" Then
newNumber = Cells(count - 1, 1)
last2() = Split(newNumber, ".")
Splitnumber = last2(3) + 1
Cells(count, 1) = last2(0) + "." + last2(1) + "." + last2(2) + "." + CStr(Splitnumber)
'Cells(count, 2) = last2(3)
End If
Next
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 36598228
But you're not filling all the gaps, e.g. between 191.192.192.5 and 191.192.194.50?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36598263
Try this VBA
Option Explicit

Sub FillIn()
    Dim r As Integer
    Dim ip4 As Integer
    Dim strFirstThree As String
    
    r = 1
    ip4 = Split(Cells(r, 1).Value, ".")(3) + 1
    strFirstThree = Left(Cells(r, 1).Value, 11)
    Do Until Cells(r, 1).Value = ""
        r = r + 1
        If Left(Cells(r, 1).Value, 11) = strFirstThree Then
            If Split(Cells(r, 1).Value, ".")(3) > ip4 Then
                Rows(r).Insert xlShiftDown
                Cells(r, 1).Value = strFirstThree & "." & ip4
            End If
        End If
        If Cells(r, 1).Value = "" Then
            Exit Sub
        End If
        ip4 = Split(Cells(r, 1).Value, ".")(3) + 1
        strFirstThree = Left(Cells(r, 1).Value, 11)
    Loop
End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:ukerandi
ID: 36598267
if you run my programme it will work,its filling the all the gaps. untill 100 what he has to do is loop extend until 255
Dim count As Long
Dim newNumber As String
Dim last2() As String
Dim Splitnumber As Long

For count = 1 To 255



If Cells(count, 1) = "" Then
newNumber = Cells(count - 1, 1)
last2() = Split(newNumber, ".")
Splitnumber = last2(3) + 1
Cells(count, 1) = last2(0) + "." + last2(1) + "." + last2(2) + "." + CStr(Splitnumber)
'Cells(count, 2) = last2(3)
End If
Next
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36598312
Oops. I have included the heading row.

Therefore line 8 should be
r = 2
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:iNc0g
ID: 36816725
I forgot to mention that on column B there's a description of the specific ip owner, meaning the data on columns a + b should stay on the same rows and keep in sync even when ip addresses fill in the gaps
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36816905
Well my macro inserts rows, so all the columns for the the rows below would move down.

ukerandi's macro assumes that there are spaces for the missing IPs so there is no row insertion.

Either way Column B should still match column A.

Have you tried the macros?
0
 

Author Comment

by:iNc0g
ID: 36908285
Worked great except I need the macro to complete the range completely, for example:

the range 191.192.194 ends at .242 , i need the macro to complete the rest up to 254 , currently it stopped at .242 because that's the last address in that scope.

191.192.194.242
--missing addresses up to 254--
191.192.195.0
191.192.195.1

Thanks.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 36914091
That's a bit more complicated, but try this.

Note that is only goes up to .65 instead of .255 for testing purposes. Change the constant value for production runs.
Option Explicit


Sub FillIn()
    Dim r As Integer
    Dim ip4 As Integer
    Dim strFirstThree As String
    Dim sh As Worksheet
    Dim rng As Range
    
    Const ip4Start = 0
    Const ip4End = 66   'test value. Needs to be 256 if live
    

    r = 1
    ip4 = ip4Start
    strFirstThree = Left(Cells(r, 1).Value, 11)
    Do Until Cells(r, 1).Value = ""
        Do While Left(Cells(r, 1).Value, 11) = strFirstThree
            Do Until Split(Cells(r, 1).Value, ".")(3) = ip4
                Rows(r).Insert xlShiftDown
                Cells(r, 1).Value = strFirstThree & "." & ip4
                ip4 = ip4 + 1
                r = r + 1
            Loop
            Do While Left(Cells(r, 1).Value, 11) = strFirstThree And Split(Cells(r, 1).Value, ".")(3) = ip4
                ip4 = ip4 + 1
                r = r + 1
                If Cells(r, 1).Value = "" Then
                    Exit Do
                End If
            Loop
        Loop
        Do Until ip4 = ip4End
            Rows(r).Insert xlShiftDown
            Cells(r, 1).Value = strFirstThree & "." & ip4
            ip4 = ip4 + 1
            r = r + 1
        Loop
        strFirstThree = Left(Cells(r, 1).Value, 11)
        ip4 = ip4Start
    Loop
    
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36914098
I've also started on the wrong row again, so line 15 should be

 r = 2
0
 

Author Comment

by:iNc0g
ID: 36914984
Worked great, thanks again.
0
 

Author Closing Comment

by:iNc0g
ID: 36914988
worked gr8.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now