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?

More than 3 years have passed since last update.

セル内の文字を抽出する

Posted at

文字の右から抽出する

見本としてA1に「あいうえお」と入力しました。
ココから関数、VBAを使用して指定の文字数を抽出します。
image.png

Excel関数

関数で抽出する場合の書き方

=RIGHT(対象セル,抽出する文字数)

入力例
=RIGHT(A1,1)

対象セルと抽出する文字数を指定する事で末尾の文字を抜き出せます。
image.png

例え抽出する文字数が元の文字数より多かったとしてもエラーになる事はありません。

ExcelVBA

VBAで抽出する場合の書き方


結果 = Right(対象セル,抽出する文字数)

書き方は上記の関数入力とほぼ同じですね。
結果のところを変数にしたり、セルを指定する事で応用が利くと思います。

以下の例文ではセルB1に直接結果を出力しています。


Sub 文字の右から抽出()
    
    'セルA1の右から2文字をセルB1に出力
    Cells(1, 2) = Right(Cells(1, 1), 2)
    
End Sub

使用例としてはクライアントPCの管理No.の末尾の数字で管理サーバが異なる場合などに重宝しました。
image.png

初めて記事を書いてみました。
不慣れではありますが、関数とVBAの比較、PowerShellのコマンドなど
自分への備忘を含めて記事にしていこうと思います。

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?