Come trovare e sostituire il testo quindi preservare la formattazione in Excel?
Trova e sostituisci conserva la formattazione con il codice macro
Trova e sostituisci conserva la formattazione con il codice macro
Non ci sono altri metodi ma il codice macro può aiutarti a trovare e sostituire il testo e preservare la formattazione.
1. Stampa Alt + F11 chiavi per aprire il Microsoft Visual Basic per l'applicazione finestra.
2. Clic inserire > Modulie incolla il codice sottostante nello script.
VBA: trova e sostituisci conserva la formattazione
Sub CharactersReplace(Rng As Range, FindText As String, ReplaceText As String, Optional MatchCase As Boolean = False)
'UpdatebyExtendoffice20160711
Dim I As Long
Dim xLenFind As Long
Dim xLenRep As Long
Dim K As Long
Dim xValue As String
Dim M As Long
Dim xCell As Range
xLenFind = Len(FindText)
xLenRep = Len(ReplaceText)
If Not MatchCase Then M = 1
For Each xCell In Rng
If VarType(xCell) = vbString Then
xValue = xCell.Value
K = 0
For I = 1 To Len(xValue)
If StrComp(Mid$(xValue, I, xLenFind), FindText, M) = 0 Then
xCell.Characters(I + K, xLenFind).Insert ReplaceText
K = K + xLenRep - xLenFind
End If
Next
End If
Next
End Sub
Sub Test_CharactersReplace()
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
Set xRg = Application.InputBox("Select a range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
Call CharactersReplace(xRg, "KK", "Kutools", True)
End Sub
3. Stampa F5 chiave, viene visualizzata una finestra di dialogo per selezionare un intervallo da trovare e sostituire, vedi screenshot:
4. Fare clic su OK e la stringa specifica nella cella selezionata viene sostituita con altra e mantiene la formattazione.
Consiglio: Nel codice, KK è la stringa che vuoi trovare e Kutools è la stringa con cui vuoi sostituire, puoi cambiarli secondo le tue necessità.
I migliori strumenti per la produttività in ufficio
Potenzia le tue competenze di Excel con Kutools per Excel e sperimenta l'efficienza come mai prima d'ora. Kutools per Excel offre oltre 300 funzionalità avanzate per aumentare la produttività e risparmiare tempo. Fai clic qui per ottenere la funzionalità di cui hai più bisogno...
Office Tab porta l'interfaccia a schede in Office e semplifica notevolmente il tuo lavoro
- Abilita la modifica e la lettura a schede in Word, Excel, PowerPoint, Publisher, Access, Visio e Project.
- Apri e crea più documenti in nuove schede della stessa finestra, piuttosto che in nuove finestre.
- Aumenta la produttività del 50% e riduce ogni giorno centinaia di clic del mouse!