Solved

VB Script or Macro needed

Posted on 2011-02-18
4
976 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
  • 2
4 Comments
 
LVL 52

Expert Comment

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

~bp
0
 
LVL 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

14 Experts available now in Live!

Get 1:1 Help Now