メインコンテンツへスキップ

テキストを見つけて置換し、Excelで書式を保持するにはどうすればよいですか?

doc find replace save format 1
doc find replace save format 2
検索と置換機能は、特定の値を見つけて別の値に置き換える必要がある場合に強力で役立ちます。 ただし、セルの範囲があり、一部のセルに異なる書式値が含まれている場合。 この場合、検索と置換機能によって値を検索して置換すると、下のスクリーンショットに示すようにセル内の値のフォーマットが壊れます。Excelで検索して置換するときに、各セルの値のフォーマットを保持するにはどうすればよいですか?

保存フォーマットを検索してマクロコードに置き換えます

矢印青い右バブル 保存フォーマットを検索してマクロコードに置き換えます

他に方法はありませんが、マクロコードを使用すると、テキストを検索して置換し、書式を保持することができます。

1。 押す Altキー+ F11 キーを押して アプリケーション用 Microsoft Visual Basic 窓。

2。 クリック インセット > モジュール、以下のコードをスクリプトに貼り付けます。

VBA:保存フォーマットを検索して置換

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 find replace save format 5

3。 押す F5 キーを押すと、検索して置き換える範囲を選択するためのダイアログが表示されます。スクリーンショットを参照してください。
doc find replace save format 6

4. [OK]をクリックすると、選択したセルの特定の文字列が他の文字列に置き換えられ、書式が保持されます。
doc find replace save format 4

先端:コードでは、  KK 見つけたい文字列であり、 クツール は置き換えたい文字列です。必要に応じて変更できます。

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
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