coordonnees_v2.ods
Bonjour,
En espérant que toutes les cellules ont le même format que celles de la ligne 2.
Dans la colonne "A" tout texte en majuscules et considéré comme le nom de famille, le reste comme prénom.
Dans la colonne "B", la macro recherche le code postal et crée l'adresse, la ville et le CP dans les cellules adjacentes. Si on ne trouve pas le code postal un message d'erreur est écrit en colonne "C".
A+
Code : Tout sélectionner
REM ***** BASIC *****
Sub reformat()
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
Dim oDoc as Object, oF1 as object, i as Long
Dim oCell as object, oCC as object
Dim oCellValue as Double
Dim x As Integer
Dim y As Integer
Dim sNom As String
Dim sPrenom As String
Dim sCP As String
Dim sVille As String
Dim sAdr As String
Dim iLigne As Integer
Dim iCol As integer
oDoc = ThisComponent
oF1=oDoc.Sheets.getbyname("Feuille1")
iLigne = 1
iCol = 0
Do While oF1.getCellByPosition(iCol,iLigne).String <> ""
oCell = oF1.getCellByPosition(iCol,iLigne)
sSplit = split(oCell.string," ",10)
' Extraire nom et prénom
For x = 0 To ubound(sSplit())
if isupper(sSplit(x)) Then
sNom = sNom + " " + sSplit(x)
Else
sPreNom = sPrenom + " " + sSplit(x)
End If
Next
' Extraction adresse
oCell = oF1.getCellByPosition(iCol+1,iLigne)
sAdresse = replace(oCell.string,chr(10)," ",1,1) 'Suppression. Retour ligne
sSplit = split(sAdresse," ",50)
y = 0
For x = 0 To ubound(sSplit())
if isnumeric(sSplit(x)) Then
If len(sSplit(x)) = 5 Then
sCP = sSplit(x)
finadr =Instr(1, oCell.string, sCP) -2
sAdr = mid(oCell.string,1,finadr)
sVille = mid(oCell.string,finadr+7,len(oCell.string)-finadr+5)
End If
End If
Next
If sCP = "" Then
oF1.getCellByPosition(2,iLigne).String = "Code postal non trouvé ou incorrect"
else
oF1.getCellByPosition(2,iLigne).String = trim(sNom)
oF1.getCellByPosition(3,iLigne).String = trim(sPrenom)
oF1.getCellByPosition(4,iLigne).String = trim(sAdr)
oF1.getCellByPosition(5,iLigne).String = trim(sVille)
oF1.getCellByPosition(6,iLigne).String = sCP
End If
iLigne = iLigne + 1
sNom = ""
sPrenom =""
sAdr = ""
sVille = ""
sCP = ""
loop
End Sub
Vous ne pouvez pas consulter les pièces jointes insérées à ce message.