Come trovare e sostituire il testo mantenendo la formattazione in Excel?
![]() |
![]() |
Trova e sostituisci mantenendo la formattazione con il codice Macro
Trova e sostituisci mantenendo la formattazione con il codice Macro
Non ci sono altri metodi oltre al codice macro che possono aiutarti a trovare e sostituire il testo mantenendo la formattazione.
1. Premi i tasti Alt + F11 per aprire la finestra Microsoft Visual Basic for Application.
2. Clicca su Inserisci > Modulo, e incolla il seguente codice nello script.
VBA: Trova e sostituisci mantenendo 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
Suggerimento: Nel codice, KK è la stringa che vuoi trovare, e Kutools è la stringa con cui vuoi sostituirla; puoi modificarle secondo necessità.
3. Premi F5 un tasto, apparirà una finestra di dialogo per selezionare un intervallo in cui trovare e sostituire, vedi screenshot:
4. Clicca OK, e la stringa specifica nelle celle selezionate verrà sostituita mantenendo la formattazione.
I migliori strumenti per la produttività in Office
Potenzia le tue competenze in Excel con Kutools per Excel e sperimenta un'efficienza mai vista prima. Kutools per Excel offre oltre300 funzionalità avanzate per aumentare la produttività e risparmiare tempo. Clicca qui per ottenere la funzione di cui hai più bisogno...
Office Tab porta l'interfaccia a schede in Office e rende il tuo lavoro molto più semplice
- 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, invece che in nuove finestre.
- Aumenta la tua produttività del50% e riduce centinaia di clic del mouse ogni giorno!