0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Excel VBA】Trim関数、LTrim関数、RTrim関数|空白を削除する方法と注意点

0
Last updated at Posted at 2025-07-07

この記事ではTrim関数、LTrim関数、RTrim関数の使い方と注意点について解説します。
他のよく使うVBA関数一覧はこちら。

Trim関数、LTrim関数、RTrim関数の使い方と注意点

Trim関数、LTrim関数、RTrim関数は、先頭と末尾もしくはどちらかのスペースを削除して返す関数です。

  • Trim関数 : 先頭と末尾のスペースを削除
  • LTrim関数 : 先頭のスペースを削除
  • RTrim関数 : 末尾のスペースを削除

スペースが複数の場合でも先頭と末尾であれば全て削除されます。

構文

Trim(文字列)
LTrim(文字列)
RTrim(文字列)
  • 文字列: スペースを削除する対象の文字列
  • 戻り値: 先頭と末尾もしくはどちらかのスペースが削除された文字列

使用例

Trim関数

こちらの例ではExcel VBA という文字列に対し、Trim、RTrim、LTrim関数を使用しています。

Sub Sample()
    Dim mystring As String
    mystring = " Excel VBA  "   ' 先頭に半角スペース1つ、末尾に半角スペース2つ
    Debug.Print "[" & Trim(mystring) & "]"
    Debug.Print "[" & LTrim(mystring) & "]"
    Debug.Print "[" & RTrim(mystring) & "]"
End Sub

▶ 出力結果
Trim関数では先頭と末尾、LTrim関数では先頭、RTrim関数では末尾のスペースが削除されています。

[Excel VBA]
[Excel VBA  ]
[ Excel VBA]

⚠️注意

先頭と末尾以外は削除されない

Trim関数、LTrim関数、RTrim関数では文字列中のスペースは削除されません。
使用例の出力部分から分かるようにExcelとVBAの間のスペースが残っています。

文字列中も含め全てのスペースを削除したい場合は、Replace 関数を使用しましょう。

その他のVBA関数

【Excel VBA】VBAでよく使う関数一覧&基本の使い方

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?