Question : VBS Excel script

Hello,

I have one excel file with 3 columns : Customer, Hostname, IP Address.
I want to perform cleanup of this excel file in the following way :
In the IP column, sometimes it is not written only the ip address ( example : CUST 192.344.108.11 ), or in some cases is written more then 1 ip address ( example : ip management   122.31.183.126 "ip servizio cliente  112.202.241.93".
I want a script to delete the extra information. The IP column should contain information only in this format : x.x.x.x
In the case that there is more then 1 ip address listed, the script will arrange it (with space between them) like this :
x.x.x.x x.x.x.x x.x.x.x
Also if there is any row that is missing one of the 3 informations, the row should be deleted.
The last requirement is that if the customer column contains spaces ( example : FSA CROSS SPA ), the script should change that to ( example : FSACROSSSPA )

Thank you

Answer : VBS Excel script

I'm pretty sure this code (in the excel spreadsheet) will do exactly what you want.

sdwalker

Please make backups before you test code found here.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
Sub PerformCleanup()

Set objRegEx = CreateObject("VBScript.RegExp")
  
objRegEx.Global = True
objRegEx.Pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  

numRows = Sheets("Sheet1").Range("A50000").End(xlUp).Row

For i = numRows To 1 Step -1

  ' delete incomplete rows
  If Range("A" & i).Value = "" Or Range("A" & i).Value = "" Or Range("A" & i).Value = "" Then
    Rows(i & ":" & i).Select
    Selection.Delete Shift:=xlUp
  End If

  ' Remove spaces in customer name
  Range("A" & i).Replace " ", ""

  ' Find and
  strSearchString = Range("C" & i).Value
  
    Set colMatches = objRegEx.Execute(strSearchString)
    
    Range("C" & i).ClearContents
    
    If colMatches.Count > 0 Then
      
        For Each strMatch In colMatches
           Range("C" & i).Value = Range("C" & i).Value & strMatch.Value & " "
        Next
        
        Range("C" & i).Value = Left(Range("C" & i).Value, Len(Range("C" & i).Value) - 1)
    End If
  
Next i

  Set objFSO = Nothing
  Set objFile = Nothing

End Sub
Random Solutions  
 
programming4us programming4us