Solved

VB Script or Macro needed

Posted on 2011-02-18
4
984 Views
Last Modified: 2012-05-11
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
Comment
Question by:seaninman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 34931345
Does the list start in B1 or B2?

~bp
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 167 total points
ID: 34931388
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
 
LVL 50

Assisted Solution

by:Ingeborg Hawighorst
Ingeborg Hawighorst earned 167 total points
ID: 34931409
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
 
LVL 45

Assisted Solution

by:patrickab
patrickab earned 166 total points
ID: 34932885
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

733 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