Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

fonction remplace dans VBA

5 réponses
Avatar
Mary
Bonjour =E0 tous...

Je sais qu'il existe une fonction remplace dans VB afin de=20
remplacer dans un String tel caract=E8re par un autre. Par=20
contre il semble que dans VBA pour la suite de travail=20
Excel 97 n'existe pas malheureusement.

J'ai trouv=E9 quelque fonction pr=E9-=E9crite sur internet...=20
par contre ca semble =EAtre assez peu performant comme=20
code...=20


Public Function ReplaceString(ByVal pString As String,=20
ByVal Keyword As String, ByVal NewKeyWord As String) As=20
String
Dim StrLen As Long
Dim StrOut As String
Dim KeyPos As Long
Dim StrLenChange As Boolean
=20
' set a few values before we go any further
StartPos =3D 1
StrLen =3D Len(pString)
=20
' if the word to replace and the word to replace by are=20
different lengths, record that they are
If Len(Keyword) <> Len(NewKeyWord) Then StrLenChange =3D=20
True
=20
' check to see where the first occurence of the keyword=20
to replace is in the string
KeyPos =3D InStr(StartPos, pString, Keyword)
=20
' only do the replace loop if the keyword string is there
While KeyPos > 0
' rebuild the string, taking out the keyword and=20
replacing it with the NewKeyword
pString =3D Left(pString, KeyPos - 1) & NewKeyWord & MID
(pString, KeyPos + Len(Keyword), StrLen - KeyPos - Len
(Keyword) + 1)
=20
' if the Keyword and NewKeyword as different lengths=20
then recalc the length of the string
If StrLenChange Then StrLen =3D Len(pString)
=20
' calculate the position where we should start looking=20
to see if the KeyWord is in the string
StartPos =3D KeyPos + Len(NewKeyWord)
=20
' check to see where the next occurence of the keyword=20
to replace is in the string
KeyPos =3D InStr(StartPos, pString, Keyword)
Wend
=20
' return the string
ReplaceString =3D pString
=20
End Function

Le besoin exacte c'est que je veux remplacer tous les VBLF=20
par un VBcrlf... La chaine comporte quelque 500000=20
charact=E8re... vu qu'elle provient d'un Line Input d'un=20
fichier. Quelqu'un =E0 une meilleur m=E9thode de recherche=20
remplace que celle pr=E9sent=E9e si dessous?

Merci

5 réponses

Avatar
Denis Michon
Bonjour Mary,

En vba, C'est la fonction "Replace" et non "Remplace" disponible à partir de la version 2000

Sur les versions antérieures d'excel, tu peux utiliser la fonction de la feuille substitue

substitue(texte;ancien_texte;nouveau_texte;no_position)

et pour l'appeler en VBA,

Letexte = Application.Substitute(texte;ancien_texte;nouveau_texte;no_position)


Salutations!


"Mary" a écrit dans le message de news:00e801c3b065$4a756960$
Bonjour à tous...

Je sais qu'il existe une fonction remplace dans VB afin de
remplacer dans un String tel caractère par un autre. Par
contre il semble que dans VBA pour la suite de travail
Excel 97 n'existe pas malheureusement.

J'ai trouvé quelque fonction pré-écrite sur internet...
par contre ca semble être assez peu performant comme
code...


Public Function ReplaceString(ByVal pString As String,
ByVal Keyword As String, ByVal NewKeyWord As String) As
String
Dim StrLen As Long
Dim StrOut As String
Dim KeyPos As Long
Dim StrLenChange As Boolean

' set a few values before we go any further
StartPos = 1
StrLen = Len(pString)

' if the word to replace and the word to replace by are
different lengths, record that they are
If Len(Keyword) <> Len(NewKeyWord) Then StrLenChange True

' check to see where the first occurence of the keyword
to replace is in the string
KeyPos = InStr(StartPos, pString, Keyword)

' only do the replace loop if the keyword string is there
While KeyPos > 0
' rebuild the string, taking out the keyword and
replacing it with the NewKeyword
pString = Left(pString, KeyPos - 1) & NewKeyWord & MID
(pString, KeyPos + Len(Keyword), StrLen - KeyPos - Len
(Keyword) + 1)

' if the Keyword and NewKeyword as different lengths
then recalc the length of the string
If StrLenChange Then StrLen = Len(pString)

' calculate the position where we should start looking
to see if the KeyWord is in the string
StartPos = KeyPos + Len(NewKeyWord)

' check to see where the next occurence of the keyword
to replace is in the string
KeyPos = InStr(StartPos, pString, Keyword)
Wend

' return the string
ReplaceString = pString

End Function

Le besoin exacte c'est que je veux remplacer tous les VBLF
par un VBcrlf... La chaine comporte quelque 500000
charactère... vu qu'elle provient d'un Line Input d'un
fichier. Quelqu'un à une meilleur méthode de recherche
remplace que celle présentée si dessous?

Merci
Avatar
Michel Gaboly
Bonsoir,

Plutôt que d'une fonction adaptée à une cellule en particulier,
tu as besoin de l'équivalent d'un Rechercher/Remplacer :

Sub Macro2()
Cells.Replace What:=vbLf, Replacement:=vbCrLf, LookAt:=xlPart
End Sub

NB - Cells.Replace doit être sur une seule ligne, qui com-
mence par "Cells" et se termine par "xlPart".



Bonjour à tous...

Je sais qu'il existe une fonction remplace dans VB afin de
remplacer dans un String tel caractère par un autre. Par
contre il semble que dans VBA pour la suite de travail
Excel 97 n'existe pas malheureusement.

J'ai trouvé quelque fonction pré-écrite sur internet...
par contre ca semble être assez peu performant comme
code...

Public Function ReplaceString(ByVal pString As String,
ByVal Keyword As String, ByVal NewKeyWord As String) As
String
Dim StrLen As Long
Dim StrOut As String
Dim KeyPos As Long
Dim StrLenChange As Boolean

' set a few values before we go any further
StartPos = 1
StrLen = Len(pString)

' if the word to replace and the word to replace by are
different lengths, record that they are
If Len(Keyword) <> Len(NewKeyWord) Then StrLenChange > True

' check to see where the first occurence of the keyword
to replace is in the string
KeyPos = InStr(StartPos, pString, Keyword)

' only do the replace loop if the keyword string is there
While KeyPos > 0
' rebuild the string, taking out the keyword and
replacing it with the NewKeyword
pString = Left(pString, KeyPos - 1) & NewKeyWord & MID
(pString, KeyPos + Len(Keyword), StrLen - KeyPos - Len
(Keyword) + 1)

' if the Keyword and NewKeyword as different lengths
then recalc the length of the string
If StrLenChange Then StrLen = Len(pString)

' calculate the position where we should start looking
to see if the KeyWord is in the string
StartPos = KeyPos + Len(NewKeyWord)

' check to see where the next occurence of the keyword
to replace is in the string
KeyPos = InStr(StartPos, pString, Keyword)
Wend

' return the string
ReplaceString = pString

End Function

Le besoin exacte c'est que je veux remplacer tous les VBLF
par un VBcrlf... La chaine comporte quelque 500000
charactère... vu qu'elle provient d'un Line Input d'un
fichier. Quelqu'un à une meilleur méthode de recherche
remplace que celle présentée si dessous?

Merci


--
Cordialement,

Michel Gaboly
http://www.gaboly.com

Avatar
gb
Salut Mary.

Pour ne pas diminuer dramatiquement les performances, il semble qu'il faille
éviter de redimensionner inutilement une longue chaîne de caractères ou de
modifier inutilement de longs segments d'une chaîne de caractères.

Voici comment on peut modifier le plus court segment possible d'une chaîne
de caractères.

Public Function RemplacerString2(ByVal pString As String, _
ByVal MotCle As String, ByVal NouveauMotCle As String) As _
String

Dim DebutPstring As Long
Dim DebutResultat As Long

Dim Occurences As Long
Dim DifferenceDeLongueur As Long
Dim str_resultat As String

Dim b As Long
Dim reste As Long
Dim strTmp As String

Dim l As Long

DebutPstring = 1
Occurences = 0
While InStr(DebutPstring, pString, MotCle) > 0
Occurences = Occurences + 1
DebutPstring = InStr(DebutPstring, pString, MotCle) + 1
Wend

DifferenceDeLongueur = Len(NouveauMotCle) - Len(MotCle)
str_resultat = Space(Len(pString) + Occurences * DifferenceDeLongueur)

DebutPstring = 1
DebutResultat = 1

b = InStr(DebutPstring, pString, MotCle)
reste = Len(pString) - b
While b > 0
strTmp = Mid(pString, DebutPstring, b - DebutPstring) & NouveauMotCle
l = Len(strTmp)

Mid(str_resultat, DebutResultat, l) = strTmp
DebutResultat = DebutResultat + l
DebutPstring = b + Len(MotCle)

reste = Len(pString) - b - Len(MotCle) + 1
b = InStr(DebutPstring, pString, MotCle)
Wend

If DebutResultat <= Len(str_resultat) Then
l = Len(Right(pString, reste))
Mid(str_resultat, DebutResultat, l) = Right(pString, reste)
End If

RemplacerString2 = str_resultat

End Function

gb


"Mary" wrote in message
news:00e801c3b065$4a756960$
Bonjour à tous...

Je sais qu'il existe une fonction remplace dans VB afin de
remplacer dans un String tel caractère par un autre. Par
contre il semble que dans VBA pour la suite de travail
Excel 97 n'existe pas malheureusement.

J'ai trouvé quelque fonction pré-écrite sur internet...
par contre ca semble être assez peu performant comme
code...


Public Function ReplaceString(ByVal pString As String,
ByVal Keyword As String, ByVal NewKeyWord As String) As
String
Dim StrLen As Long
Dim StrOut As String
Dim KeyPos As Long
Dim StrLenChange As Boolean

' set a few values before we go any further
StartPos = 1
StrLen = Len(pString)

' if the word to replace and the word to replace by are
different lengths, record that they are
If Len(Keyword) <> Len(NewKeyWord) Then StrLenChange True

' check to see where the first occurence of the keyword
to replace is in the string
KeyPos = InStr(StartPos, pString, Keyword)

' only do the replace loop if the keyword string is there
While KeyPos > 0
' rebuild the string, taking out the keyword and
replacing it with the NewKeyword
pString = Left(pString, KeyPos - 1) & NewKeyWord & MID
(pString, KeyPos + Len(Keyword), StrLen - KeyPos - Len
(Keyword) + 1)

' if the Keyword and NewKeyword as different lengths
then recalc the length of the string
If StrLenChange Then StrLen = Len(pString)

' calculate the position where we should start looking
to see if the KeyWord is in the string
StartPos = KeyPos + Len(NewKeyWord)

' check to see where the next occurence of the keyword
to replace is in the string
KeyPos = InStr(StartPos, pString, Keyword)
Wend

' return the string
ReplaceString = pString

End Function

Le besoin exacte c'est que je veux remplacer tous les VBLF
par un VBcrlf... La chaine comporte quelque 500000
charactère... vu qu'elle provient d'un Line Input d'un
fichier. Quelqu'un à une meilleur méthode de recherche
remplace que celle présentée si dessous?

Merci
Avatar
Daniel.M
Salut,

Il faudrait prévoir le cas où MotCle est vide et sortir gracieusement.
C'est vrai aussi de la fonction ReplaceString publiée initialement.

Salutations,

Daniel M.
Avatar
gb
Salut Daniel.
Bonne idée.

Public Function RemplacerString2(ByVal pString As String, _
ByVal MotCle As String, ByVal NouveauMotCle As String) As _
String

'La variable pString et la variable MotCle ne peuvent être vides
If pString = "" Or MotCle = "" Then
RemplacerString2 = pString
Exit Function
End If

Dim DebutPstring As Long
Dim DebutResultat As Long

Dim Occurences As Long
Dim DifferenceDeLongueur As Long
Dim str_resultat As String

Dim b As Long
Dim reste As Long
Dim strTmp As String

Dim l As Long

DebutPstring = 1
Occurences = 0
While InStr(DebutPstring, pString, MotCle) > 0
Occurences = Occurences + 1
DebutPstring = InStr(DebutPstring, pString, MotCle) + 1
Wend

DifferenceDeLongueur = Len(NouveauMotCle) - Len(MotCle)
str_resultat = Space(Len(pString) + Occurences * DifferenceDeLongueur)

DebutPstring = 1
DebutResultat = 1

b = InStr(DebutPstring, pString, MotCle)
reste = Len(pString) - b
While b > 0
strTmp = Mid(pString, DebutPstring, b - DebutPstring) & NouveauMotCle
l = Len(strTmp)

Mid(str_resultat, DebutResultat, l) = strTmp
DebutResultat = DebutResultat + l
DebutPstring = b + Len(MotCle)

reste = Len(pString) - b - Len(MotCle) + 1
b = InStr(DebutPstring, pString, MotCle)
Wend

If DebutResultat <= Len(str_resultat) Then
l = Len(Right(pString, reste))
Mid(str_resultat, DebutResultat, l) = Right(pString, reste)
End If

RemplacerString2 = str_resultat

End Function

"Daniel.M" wrote in message
news:
Salut,

Il faudrait prévoir le cas où MotCle est vide et sortir gracieusement.
C'est vrai aussi de la fonction ReplaceString publiée initialement.

Salutations,

Daniel M.