Een familie van Microsoft-spreadsheetsoftware met hulpmiddelen voor het analyseren, in kaart brengen en communiceren van gegevens.
Jan,
Ik had je voorspelling gezien maar ben er niet dieper op ingegaan. De PQ oefening was maar een poging om mezelf wat bij te schaven en omdat dit lukte heb ik ze ook maar geplaatst. Als "wijsneus" wou ik ze dan ook plaatsen. ;)
Uit de testen blijkt ook dat de oplossing van Peter niet 100% is. Met mijn Unix/Linux achtergrond begon ik aan regular expressions te denken. Het resultaat:
| Adres | Nummer | Straat |
|---|---|---|
| Adm. de Ruyterstraat 34 | 34 | Adm. de Ruyterstraat |
| Pieter Stastokerf 8 | 8 | Pieter Stastokerf |
| B. van Zuylenstraat 312 | 312 | B. van Zuylenstraat |
| 2e Helmerstraat 14 | 14 | 2e Helmerstraat |
| Willem 2 plein | Willem 2 plein | |
| Willem 2 plein 7 | 7 | Willem 2 plein |
| B. van Zuylenstraat 312 Bus 5 | 312 Bus 5 | B. van Zuylenstraat |
| Zwanestraat | Zwanestraat |
De formules:
Nummer: =RegExp_Extract(A2;"(\d+$|\d+ Bus \d+$)";"$1")
Straat: =RegExp_Replace(A2;B2;"")
(\d+$|\d+ Bus \d+$) kan je vertalen als "een aantal digits (1 of meer) op het einde" of (|) "een aantal digits ""Bus"" en een aantal digits op het einde". En geef het resultaat (als $1).
Je zal de code wat moeten tweaken om bruikbaar te maken, komt uit mijn persoonlijke add-in.
Met RegExp_Replace kan ook de vraag van Alexandra (al is die in de vraag nogal onduidelijk) opgelost worden. Het pattern zou dan iets moeten zijn als \d{4}\w{2}. (4 cijfers, 2 letters)
Public Function RegExp_Replace(Expression As Variant, pattern As String, ReplaceStr As String, Optional ByVal Index, Optional ByVal IgnoreCase, Optional ByVal FullMatch) As Variant
Dim objRegEx As New RegExp
Dim objMatch As MatchCollection
Dim vbeCompareMethod As VbCompareMethod
Dim strResult As String
On Error GoTo Error_RegExp_Replace
If IsMissing(IgnoreCase) Then IgnoreCase = True
If IsMissing(FullMatch) Then FullMatch = True
If IsMissing(Index) Then Index = 0
pattern = Replace(pattern, ";", ",")
If IgnoreCase Then
vbeCompareMethod = vbTextCompare
Else
vbeCompareMethod = vbBinaryCompare
End If
objRegEx.IgnoreCase = IgnoreCase
objRegEx.Global = FullMatch
objRegEx.pattern = pattern
If objRegEx.test(Expression) Then
Set objMatch = objRegEx.Execute(Expression)
If Index = 0 Then
strResult = objRegEx.Replace(Expression, ReplaceStr)
Else
If Index - 1 >= objMatch.Count Then Index = objMatch.Count
strResult = Left(Expression, objMatch(Index - 1).FirstIndex) & Replace(Expression, objMatch(Index - 1), ReplaceStr, objMatch(Index - 1).FirstIndex + 1, 1, vbeCompareMethod)
End If
Else
''' modified 12/09/2015, return the expression iso empty string.
strResult = Expression
End If
RegExp_Replace = strResult
Exit_Function:
''' reset changed applications settings here
Set objMatch = Nothing
Set objRegEx = Nothing
Exit Function
Error_RegExp_Replace:
Select Case Err.Number
Case 18
If TypeName(Application.Caller) = "Variant()" Then
Err.Raise (18)
Else
MsgBox "User has interrupted running procedure", Title:=JPE_APP_TITLE
End If
Resume Exit_Function
Case Else
RegExp_Replace = CVErr(xlErrRef)
Resume Exit_Function
End Select
End Function
Public Function RegExp_Find(Expression As Variant, pattern As String, Optional ByVal Index, Optional ByVal IgnoreCase, Optional ByVal FullMatch) As Variant
Dim objRegEx As New RegExp
Dim objMatch As MatchCollection
Dim intResult As Integer
On Error GoTo Error_RegExp_Find
If IsMissing(IgnoreCase) Then IgnoreCase = True
If IsMissing(FullMatch) Then FullMatch = True
If IsMissing(Index) Then Index = 1
pattern = Replace(pattern, ";", ",")
objRegEx.IgnoreCase = IgnoreCase
objRegEx.Global = FullMatch
objRegEx.pattern = pattern
If objRegEx.test(Expression) Then
Set objMatch = objRegEx.Execute(Expression)
If Index - 1 >= objMatch.Count Then
intResult = 0
Else
intResult = objMatch(Index - 1).FirstIndex + 1 ''' zero based
End If
Else
intResult = 0
End If
RegExp_Find = intResult
Exit_Function:
''' reset changed applications settings here
Set objMatch = Nothing
Set objRegEx = Nothing
Exit Function
Error_RegExp_Find:
Select Case Err.Number
Case 18
If TypeName(Application.Caller) = "Variant()" Then
Err.Raise (18)
Else
MsgBox "User has interrupted running procedure", Title:=JPE_APP_TITLE
End If
Resume Exit_Function
Case Else
RegExp_Find = CVErr(xlErrRef)
Resume Exit_Function
End Select
End Function
Public Function RegExp_Test(Expression As Variant, pattern As String, Optional ByVal IgnoreCase, Optional ByVal FullMatch) As Variant
Dim blnResult As Boolean
Dim objRegEx As New RegExp
On Error GoTo Error_RegExp_Test
If IsMissing(IgnoreCase) Then IgnoreCase = True
If IsMissing(FullMatch) Then FullMatch = True
pattern = Replace(pattern, ";", ",")
objRegEx.IgnoreCase = IgnoreCase
objRegEx.Global = FullMatch
objRegEx.pattern = pattern
If objRegEx.test(Expression) Then
blnResult = True
Else
blnResult = False
End If
RegExp_Test = blnResult
Exit_Function:
''' reset changed applications settings here
Set objRegEx = Nothing
Exit Function
Error_RegExp_Test:
Select Case Err.Number
Case 18
If TypeName(Application.Caller) = "Variant()" Then
Err.Raise (18)
Else
MsgBox "User has interrupted running procedure", Title:=JPE_APP_TITLE
End If
Resume Exit_Function
Case Else
RegExp_Test = CVErr(xlErrRef)
Resume Exit_Function
End Select
End Function