[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB Script or Macro needed

Posted on 2011-02-18
4
Medium Priority
?
992 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 57

Expert Comment

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

~bp
0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 668 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 (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 668 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 664 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

656 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