[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 993
  • Last Modified:

VB Script or Macro needed

I have a spreadsheet that has a list of IP's in column B.  I am needing to run a script against that list and create a new excel sheet that contains the information below for each IP.

If I have a list of the following IPs
192.36.24.5
168.98.45.3

I need the script to create the following output for each of those IPs
192.36.24.2 - 192.36.24.30
192.36.24.34 - 192.36.24.62
192.36.24.66 - 192.36.24.94
192.36.24.242 - 192.36.24.246
192.36.24.245

\\Blank Line//

168.98.45.2 - 168.98.45.30
168.98.45.34 - 168.98.45.62
168.98.45.66 - 168.98.45.94
168.98.45.242 - 168.98.45.246
168.98.45.254

\\Blank Line//
0
seaninman
Asked:
seaninman
  • 2
3 Solutions
 
Bill PrewCommented:
Does the list start in B1 or B2?

~bp
0
 
Bill PrewCommented:
How about something like this, save as VBS.

Const sExcel = "C:\ee\EE26831341\EE26831341.xls"
Const xlUp = -4162

Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(sExcel, False, False)
oExcel.Visible = True
Set oSheet = oBook.Sheets(1)
Set oNewSheet = oBook.Sheets.Add

iNewRow = 0
For iRow = 1 To oSheet.Cells(65536, "B").End(xlUp).Row
   sBase = oSheet.Cells(iRow, "B").Value
   sBase = Left(sBase, InstrRev(sBase, "."))
   iNewRow = iNewRow + 1
   oNewSheet.Cells(iNewRow, "A").Value = sBase & "2 - " & sBase & "30"
   iNewRow = iNewRow + 1
   oNewSheet.Cells(iNewRow, "A").Value = sBase & "34 - " & sBase & "62"
   iNewRow = iNewRow + 1
   oNewSheet.Cells(iNewRow, "A").Value = sBase & "66 - " & sBase & "94"
   iNewRow = iNewRow + 1
   oNewSheet.Cells(iNewRow, "A").Value = sBase & "242 - " & sBase & "246"
   iNewRow = iNewRow + 1
   oNewSheet.Cells(iNewRow, "A").Value = sBase & "245"
   iNewRow = iNewRow + 1
Next

''oExcel.Quit
''Set oExcel = Nothing

Open in new window

~bp
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

the code below goes into a standard Excel VBA module. See attached.

Option Explicit

Sub test()
Dim source As Worksheet
Dim target As Worksheet

Dim cel As Range

Dim astr As String
Dim aint As Integer
Dim tRow As Long

Set source = Sheets("Sheet1")
Sheets.Add After:=Sheets(Sheets.Count)
Set target = Sheets(ActiveSheet.Name)

tRow = 1
For Each cel In source.Range("B1", source.Cells(Rows.Count, "B").End(xlUp))
    astr = cel.Text
    aint = InStrRev(astr, ".")
    astr = Left(astr, aint)
    target.Range("A" & tRow) = astr & "2 - " & astr & "30"
    target.Range("A" & tRow + 1) = astr & "34 - " & astr & "62"
    target.Range("A" & tRow + 2) = astr & "66 - " & astr & "94"
    target.Range("A" & tRow + 3) = astr & "242 - " & astr & "246"
    target.Range("A" & tRow + 4) = astr & "245"
    tRow = tRow + 6
Next cel
End Sub

Open in new window



cheers, teylyn
Book5.xls
0
 
patrickabCommented:
seaninman,

Perhaps the code below gets close to doing what you want - it's in the attached file.

Press the button on Sheet1 to run the macro.

Patrick
Sub IPs()
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Dim celle As Range
Dim strbase() As String
Dim baseIP As String
Dim localIP() As Long
Dim lastrow As Long
Dim i As Long
Dim x As Long

With Sheets("Sheet1")
    Set rng = Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp))
    Set rng2 = Range(.Cells(1, "B"), .Cells(.Rows.Count, "B").End(xlUp))
    Set rng3 = Range(.Cells(1, "D"), .Cells(.Rows.Count, "D").End(xlUp))
    rng3.ClearContents
    lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row

ReDim localIP(lastrow)
i = 0
x = 1
For Each celle In rng2
    localIP(i) = celle
    i = i + 1
Next celle

For Each celle In rng
    strbase = Split(celle, ".")
    For i = 0 To UBound(strbase) - 1
        baseIP = baseIP & strbase(i) & "."
    Next i
    For i = 0 To lastrow - 1
        On Error Resume Next
        If localIP(i) <> 254 Then
            .Cells(i + 1 + x, "D") = baseIP & localIP(i) & " - " & baseIP & localIP(i + 1)
        Else
            .Cells(i + 1 + x, "D") = baseIP & localIP(i)
        End If
         If i = lastrow - 1 Then x = x + lastrow + 1
    Next i
Next celle

End With

End Sub

Open in new window

Split-IPs-01.xls
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now