Vai al contenuto principale

Come trovare e sostituire il testo quindi preservare la formattazione in Excel?

doc trova sostituisci conserva formato 1
doc trova sostituisci conserva formato 2
La funzione Trova e sostituisci è potente e utile quando è necessario trovare un valore specifico e sostituirlo con un altro. Ma se è presente un intervallo di celle e alcune delle celle includono valori di formattazione diversi. In questo caso, trovare e sostituire il valore con la funzione Trova e sostituisci interromperà la formattazione dei valori nelle celle come mostrato nell'immagine sottostante, come possiamo preservare la formattazione dei valori in ogni cella durante la ricerca e la sostituzione in Excel?

Trova e sostituisci conserva la formattazione con il codice macro

freccia blu freccia destra 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

 

doc trova sostituisci conserva formato 5

3. Stampa F5 chiave, viene visualizzata una finestra di dialogo per selezionare un intervallo da trovare e sostituire, vedi screenshot:
doc trova sostituisci conserva formato 6

4. Fare clic su OK e la stringa specifica nella cella selezionata viene sostituita con altra e mantiene la formattazione.
doc trova sostituisci conserva formato 4

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

🤖 Assistente AI di Kutools: Rivoluziona l'analisi dei dati basandosi su: Esecuzione intelligente   |  Genera codice  |  Crea formule personalizzate  |  Analizzare i dati e generare grafici  |  Richiama le funzioni di Kutools...
Funzioni popolari: Trova, evidenzia o identifica i duplicati   |  Elimina righe vuote   |  Combina colonne o celle senza perdere dati   |   Round senza formula ...
Super ricerca: VLookup a criteri multipli    VLookup a valori multipli  |   VLookup su più fogli   |   Ricerca fuzzy ....
Elenco a discesa avanzato: Crea rapidamente un elenco a discesa   |  Elenco a discesa dipendente   |  Elenco a discesa a selezione multipla ....
Gestore di colonna: Aggiungi un numero specifico di colonne  |  Sposta colonne  |  Attiva/disattiva lo stato di visibilità delle colonne nascoste  |  Confronta intervalli e colonne ...
Funzionalità in primo piano: Messa a fuoco della griglia   |  Vista di progettazione   |   Grande barra delle formule    Gestore di cartelle di lavoro e fogli   |  Resource Library (Testo automatico)   |  Date picker   |  Combina fogli di lavoro   |  Crittografa/decrittografa le celle    Invia e-mail per elenco   |  Super filtro   |   Filtro speciale (filtro grassetto/corsivo/barrato...) ...
I 15 migliori set di strumenti12 Testo Strumenti (aggiungi testo, Rimuovi personaggi, ...)   |   50+ Grafico Tipi (Diagramma di Gantt, ...)   |   40+ Pratico Formule (Calcola l'età in base al compleanno, ...)   |   19 Inserimento Strumenti (Inserisci il codice QR, Inserisci immagine dal percorso, ...)   |   12 Conversione Strumenti (Numeri in parole, Conversione di valuta, ...)   |   7 Unisci e dividi Strumenti (Combina righe avanzate, Celle divise, ...)   |   ... e altro ancora

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...

Descrizione


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!
Comments (6)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Thanks, it has worked wonders for me. I needed to replace a colon with a newline character, but all the text before the colon should stay bold caption, where the rest of it should be left in regular. It has worked and abridged my work today.
Though I didn't try with cells with more than 255 characters, as it seems to be an issue with other people commenting.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Can you clarify if there is a solution that works in cells with more than 255 characters, or is this a limitation of Excel? Does your add-in support cells with more than 255 characters that contain formatting?
This comment was minimized by the moderator on the site
Same Question as John Birk i need a workaround with Cells that have more then 255 signs.
This comment was minimized by the moderator on the site
The method only work while characters less than 255 digits.
This comment was minimized by the moderator on the site
I am getting syntax error in VBA
This comment was minimized by the moderator on the site
I tried your solution on a cell that is over 255 characters long and nothing changes. Is there a workaround for this case?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations