
Option Explicit
Sub split_address()
Dim cell As Range, v As Variant
'11, Localita Incugnate - 20060 Pozzuolo Martesana (MI)
On Error GoTo gest_err
For Each cell In Range("A:A")
If Trim(cell) = "" Then Exit For
v = Split(cell, ",")
Cells(cell.Row, "B") = "'" & LTrim(v(0)) 'numero civico
v = Split(v(1), "-")
Cells(cell.Row, "C") = LTrim(v(0)) 'loc, piazza, via
v = v(1)
Cells(cell.Row, "D") = Left(LTrim(v), 5) 'cap
v = Split(Trim(Mid(v, 7)), "(")
Cells(cell.Row, "E") = LTrim(v(0)) 'comune
Cells(cell.Row, "F") = Left(v(1), 2) 'provincia
resume_here:
Next
MsgBox "Ho finito!"
Exit Sub
gest_err:
Cells(cell.Row, "B") = "* * * ERRORE * * *"
Resume resume_here
End Sub
|
Sub split_address_regexp()
Dim regex As Object, cell As Range, v As Variant, i As Integer
Range("B:F").ClearContents
Set regex = CreateObject("VBScript.Regexp")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "d+|(w+s+)+|(?!()[A-Z][A-Z]"
End With
For Each cell In Range("A:A")
If Trim(cell) = "" Then Exit For
If regex.test(cell.Value) Then
i = 1
For Each v In regex.Execute(cell.Value)
i = i + 1
Cells(cell.Row, i) = v
Next
Else
Cells(cell.Row, "B") = "* * * ERRORE * * *"
End If
Next
End Sub |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub split_address_regexp()
Dim regex As Object, cell As Range, k As Long, i As Integer
Dim oMatch As Object
Range("B:F").ClearContents
Set regex = CreateObject("VBScript.Regexp")
With regex
.Global = True
.MultiLine = False
.IgnoreCase = True
End With
For Each cell In Range("A:A")
If Trim(cell) = "" Then Exit For
regex.Pattern = "(^d*/*w*,)"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 1) = "'" & Replace(oMatch(0), ",", "")
Else
cell.Offset(0, 1) = "n. civico non presente"
End If
regex.Pattern = "([a-z])+(?!(,)+)([a-z.])+( |.)+(?"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 2) = Replace(oMatch(0), " -", "")
Else
cell.Offset(0, 2) = "via non presente"
End If
regex.Pattern = "(?:[-]{1} )(w+ )+"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 3) = Replace(oMatch(0), "- ", "")
Else
cell.Offset(0, 3) = "città non presente"
End If
regex.Pattern = "([a-z]{2})"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 4) = oMatch(0)
Else
cell.Offset(0, 4) = "provincia non presente"
End If
Next
Set oMatch = Nothing
Set regex = Nothing
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Sub split_address_regexp()
Dim regex As Object, cell As Range, k As Long, i As Integer
Dim oMatch As Object
Range("B:F").ClearContents
Set regex = CreateObject("VBScript.Regexp")
With regex
.Global = True
.MultiLine = False
.IgnoreCase = True
End With
For Each cell In Range("A:A")
If Trim(cell) = "" Then Exit For
regex.Pattern = "(^d*/*w*,)"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 1) = "'" & Replace(oMatch(0), ",", "")
Else
cell.Offset(0, 1) = "n. civico non presente"
End If
regex.Pattern = "([a-z])+(?!(,)+)([a-z.])+( |.)+-"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 2) = Replace(oMatch(0), " -", "")
Else
cell.Offset(0, 2) = "via non presente"
End If
regex.Pattern = "(?:[-]{1} )(w+ )+"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 3) = Replace(oMatch(0), "- ", "")
Else
cell.Offset(0, 3) = "città non presente"
End If
regex.Pattern = "([a-z]{2})"
If regex.test(cell.Value) Then
Set oMatch = regex.Execute(cell.Value)
cell.Offset(0, 4) = oMatch(0)
Else
cell.Offset(0, 4) = "provincia non presente"
End If
Next
Set oMatch = Nothing
Set regex = Nothing
End Sub
|
