Excel のVisual Basic for Applicationsで左、右、中、および len の各関数を使用する
概要
この記事では、Microsoft Excel の Microsoft Visual Basic for Applicationsで Left、Right、Mid、Len の各関数を使用してテキスト文字列を操作する方法の例を示します。
詳細
Microsoft は、例示のみを目的としてプログラミング例を提供しており、明示または黙示にかかわらず、いかなる責任も負わないものとします。 これには、市販性または特定の目的との適合性についての黙示の保証も含まれますが、これに限定はされません。 この記事は、説明されているプログラミング言語、手順を作成およびデバッグするために使用されているツールに読者が精通していることを前提にしています。 Microsoft のサポート エンジニアが、特定の手順の機能をわかりやすく説明します。 ユーザー固有の要件に合わせた機能の追加、プロシージャの作成などの内容変更は行っておりません。
次の手順では、Microsoft Excel で Microsoft Visual Basic for Applicationsの Left、Right、Mid、Len の各関数を使用する方法を示します。
空白の新しい Excel ブックを作成します。
Alt キーを押しながら F11 キーを押して Visual Basic エディターを開きます。
[挿入] メニューの [モジュール] をクリックします。
新しいモジュール シートに次のマクロを入力します。
Sub String_Len() ' Sets MyString. MyString = InputBox("Enter some text.") ' Displays length of string. MsgBox Prompt:="The length of the string is " & _ Len(MyString) & " characters." End Sub Sub String_Left() ' Sets MyString. MyString = InputBox("Enter some text.") StringLen = Len(MyString) Pos = InputBox("Please enter a number from 1 to " & StringLen) ' Takes the left number of specified characters. Result = Left(MyString, Pos) ' Displays the result. MsgBox Prompt:="The left " & Pos & " characters of """ & _ MyString & """ are: " & _ Chr(13) & Result End Sub Sub String_Right() ' Sets MyString. MyString = InputBox("Enter some text.") StringLen = Len(MyString) Pos = InputBox("Please enter a number from 1 to " & StringLen) ' Takes the right number of specified digits. Result = Right(MyString, Pos) ' Displays the result. MsgBox Prompt:="The right " & Pos & " characters of """ & _ MyString & """ are: " & _ Chr(13) & Result End Sub Sub String_Mid() ' Sets MyString. MyString = InputBox("Enter some text.") ' Sets starting position. StartPos = InputBox _ ("Give me a starting position (1 to " _ & Len(MyString) & ")") ' Determines length of string of text. StringLen = Len(MyString) - StartPos + 1 ' Sets number of characters. NumChars = InputBox _ ("How many characters would you like? (From 1 to " & _ StringLen & ")") MsgBox prompt:="The result is: " & _ Mid(MyString, StartPos, NumChars) End Sub
Left、Right、Mid、Len の各関数の例を確認するには、実行している Excel のバージョンに応じて、次のいずれかの手順を使用します。
- Microsoft Office Excel 2007 で、[開発者] タブをクリックし、[コード] グループの [マクロ] をクリックし、目的の関数のマクロを選択して、[実行] をクリックします。
- Microsoft Office Excel 2003 以前のバージョンの Excel では、[ツール] メニューの [マクロ] をクリックし、目的の関数のマクロを選択し、[実行] をクリックします。
関連情報
これらの関数の詳細については、モジュール シートに次のテキストを入力します。
- Len
- Right
- Left
- Mid
詳細情報が必要な関数を強調表示し、F1 キーを押します。
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示