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】Right関数|文字列の右側を取り出す方法と注意点

Last updated at Posted at 2025-07-07

Right関数の使い方と注意点

Right関数は、文字列の末尾から指定された文字数の文字列を返す関数です。
全角・半角・記号・日本語を問わず、文字単位で処理されます。

構文

Right(文字列, 文字数)
  • 文字列: 対象となる文字列やセルの値
  • 文字数: 末尾から取得したい文字数(整数)

使用例

以下は、さまざまな文字種を対象に Right 関数を使った例です。

Sub Sample()
    Dim mystring1 As String
    mystring1 = "Abc"
    Debug.Print "半角英字 : " & Right(mystring1, 2)

    Dim mystring2 As String
    mystring2 = "Abc"
    Debug.Print "全角英字 : " & Right(mystring2, 2)

    Dim mystring3 As String
    mystring3 = "あいう"
    Debug.Print "ひらがな : " & Right(mystring3, 2)

    Dim mystring4 As String
    mystring4 = "亜伊宇"
    Debug.Print "漢字 : " & Right(mystring4, 2)

    Dim mystring5 As String
    mystring5 = "!?#"
    Debug.Print "半角記号 : " & Right(mystring5, 2)

    Dim mystring6 As String
    mystring6 = "!?#"
    Debug.Print "全角記号 : " & Right(mystring6, 2)

    Dim mystring7 As String
    mystring7 = "123"
    Debug.Print "半角数字 : " & Right(mystring7, 2)
    
    Dim mystring8 As String
    mystring8 = "123"
    Debug.Print "全角数字 : " & Right(mystring8, 2)

    Dim mystring9 As String
    mystring9 = "A b C"    ' 半角スペース+全角スペース
    Debug.Print "スペース : [" & Right(mystring9, 2) & "]"
End Sub

▶ 出力結果

半角英字 : bc
全角英字 : bc
ひらがな : いう
漢字 : 伊宇
半角記号 : ?#
全角記号 : ?#
半角数字 : 23
全角数字 : 23
スペース : [ C]

⚠️注意

文字列が指定した文字数より短い場合、文字列全体が返ります。

Sub Sample()
    Dim mystring As String
    mystring = "Excel"
    Debug.Print Right(mystring, 10)
End Sub

▶ 出力結果

Excel

文字数 に 0 を指定すると、空文字が返ります。

Sub Sample()
    Dim stmystringr As String
    mystring = "Excel"
    Debug.Print "文字列[" & Right(mystring, 0) & "]"
End Sub

▶ 出力結果

文字列[]

マイナスの値を指定すると、エラーになります。

Sub Sample()
    Dim mystring As String
    mystring = "Excel"
    Debug.Print Right(mystring, -1)
End Sub

▶ 出力結果
実行時エラーになります。
error.png

その他の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?