LoginSignup
19
22

More than 5 years have passed since last update.

図形内の文字を置換するExcelマクロ

Last updated at Posted at 2015-06-02

やりたいこと

Excelってセル内は置換/検索できるけど、図形内の文字は置換/検索できない。そこで、図形内の文字を置換するマクロを作成した。

コード

下記のコードを、標準モジュールにコピペすれば使えます。

Module1
Option Explicit

Public Function RepText(ByRef findStr As String, ByRef repStr As String)

    Dim sh As Shape

    For Each sh In ActiveSheet.Shapes
        If sh.TextFrame2.HasText = msoTrue Then
            sh.TextFrame2.TextRange.Text = Replace(sh.TextFrame2.TextRange.Text, findStr, repStr)
        End If
    Next

End Function

使い方

Excelシートから使う場合だと、下記のようにセルに記入して、エンターを押せば、「りんご」と「みかん」というテキストが置換されます。

置換前
図1.png

置換後
図2.png

戻り値について

この関数は戻り値がないので、成功すると0が表示されるようです。マクロ実行後は消してもよいです。

19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22