Solved

VB Script or Macro needed

Posted on 2011-02-18
4
974 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 51

Expert Comment

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

~bp
0
 
LVL 51

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:teylyn
teylyn 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

705 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

21 Experts available now in Live!

Get 1:1 Help Now