ich möchte per VBA einzelne Wörter in einem Text ersetzten, habe mir dafür die Replace-Funktion rausgepickt.
Ein Ausgangstext wird von Zelle A1 nach A2 kopiert und der Text in A1 bleibt im Original erhalten.
In A3 steht das Suchwort Otto und in A4 ein weiteres Suchwort Max.
Der Text in A2 soll nach und nach durchlaufen und das Suchwort Otto durch ein # ersetzt werden, allerding nacheinander und nicht jedes Otto auf einen Schlag.
Das Wort Ottos soll dabei aber nicht ersetzt werden weil die Randbedingung mit dem zusätzlichen s am Ende nicht zutrifft.
Ich möchte folgendes Ergebnis haben: # Ottos #.
Leider bekomme ich folgendes Ergebnis: # #s #.
Ich steh ein bissel auf dem Schlauch. Wenn ich die Anfang meiner Replace-Funtion verändere, wird die erste # weggeschnitten.
Was muss ich verändern um mein gewünschtes Ergebnis zu bekommen?
Sub Ersetzten()
Dim strText As String 'Text, welcher durchsucht werden soll
Dim strSuchwort As String 'Wort, welches gesucht werden soll
Dim arrGefunden(0 To 6, 2) As String 'Array, um Suchergebnis zu speichern
Dim intLaufArr As Integer 'Laufvaraibel für Array
Dim intLaufSpalteSuchwort As Integer 'Laufvariable für For
Dim intStartpositionSuche As Integer 'Suchposition
'Text von Zelle A1 nach A2 kopieren
'Text in Zelle A1 bleibt im Original erhalten, Text in Zelle A2 wird verändert
With Sheets("Tabelle1")
.Cells(1, 1).Copy .Cells(1, 2)
End With
'Werte zuweisen
ThisWorkbook.Worksheets("Tabelle1").Activate
intLaufSpalteSuchwort = 3
strText = Cells(1, 2).Value
'Schleife zum Spaltenwechsel
For intLaufSpalteSuchwort = 3 To 4
strSuchwort = Cells(1, intLaufSpalteSuchwort).Value
intStartpositionSuche = 1
'Do Loop bis Suchwort nicht mehr im Text zu finden ist
Do Until InStr(intStartpositionSuche, strText, strSuchwort) = 0
'If Schleife zum Suchen und Erstezten
If InStr(intStartpositionSuche, strText, strSuchwort) > 0 Then
'Randbedingung, gucken ob ein bestimmter Wert nach dem Suchwort vorhanden ist.
If Mid(strText, (InStr(intStartpositionSuche, strText, strSuchwort)) + (Len(strSuchwort)) _
, 1) = " " _
Or Mid(strText, (InStr(intStartpositionSuche, strText, strSuchwort)) + (Len(strSuchwort)) _
, 1) = "," _
Or Mid(strText, (InStr(intStartpositionSuche, strText, strSuchwort)) + (Len(strSuchwort)) _
, 1) = "." _
Or Mid(strText, (InStr(intStartpositionSuche, strText, strSuchwort)) + (Len(strSuchwort)) _
, 1) = "!" _
Or Mid(strText, (InStr(intStartpositionSuche, strText, strSuchwort)) + (Len(strSuchwort)) _
, 1) = "?" Then
'Wenn ja, dann Speichen im Array:
'Suchwort
arrGefunden(intLaufArr, 0) = strSuchwort
'Anfangsposition des Suchwortes
arrGefunden(intLaufArr, 1) = InStr(intStartpositionSuche, strText, strSuchwort)
'Länge des Suchwortes
arrGefunden(intLaufArr, 2) = Len(strSuchwort)
'Laufvariable für Array hochzählen
intLaufArr = intLaufArr + 1
'Zelle A2 Suchwort ersetzten
Cells(1, 2).Value = Replace(strText, strSuchwort, "#", , 1)
'Suchtext aktualisieren für nächste Suche
strText = Cells(1, 2).Value
'Startposition aktualisieren
intStartpositionSuche = 1
'Wenn kein Suchwort mit Randbedingungen gefunden, dann Startposition hochzählen
Else: intStartpositionSuche = InStr(intStartpositionSuche, strText, strSuchwort) + 1
End If
End If
Loop
Next intLaufSpalteSuchwort
End Sub