VBA TextBox | VBAユーザーフォームにTextBoxを挿入して使用する方法は?

Excel VBA TextBox

テキストボックスには、単にユーザからの入力を取得するために使用された箱のようなものです、テキストボックスはユーザーフォームの一部であり、任意のExcelワークシートで[開発]タブでは、我々はユーザーフォームにテキストボックスを作成したい場合は、我々は、テキストボックスのオプションを選択することができますVBAまたはワークシートのユーザーフォームコントロールから、[デザイン]タブから選択できます。

VBA TextBoxは、ユーザーフォームの多くのコントロールのコントロールの1つです。ユーザーフォームにテキストボックスを表示することで、テキストボックスにデータを入力するように依頼でき、ユーザーが入力したデータを簡単なコードでワークシートに保存できます。

ユーザーフォームは、VBAコーディングで非常に魅力的です。これは、特にユーザーからの入力を取得する必要がある場合に非常に役立ちます。ユーザーフォームには多くのコントロールがあり、ユーザーから入力値を取得するには、「テキストボックス」がユーザーフォームの理想的なオプションです。ユーザーフォームにテキストボックスを配置することで、表示しているテキストボックスに必要な値を入力するようにユーザーに実際に指示できます。VBAテキストボックスについてわからない場合は、この記事でVBAテキストボックスのツアーを行います。

VBAユーザーフォームにTextBoxを挿入する方法は?

このVBAテキストボックステンプレートはここからダウンロードできます–VBAテキストボックステンプレート

最初にテキストボックスを挿入するには、ユーザーフォームを挿入する必要があります。ユーザーフォームを挿入するには、[挿入]> [ユーザーフォーム]に移動します。

ユーザーフォームをクリックするとすぐに、別のモジュールと同じようにユーザーフォームが表示されます。

ユーザーフォームをクリックして、ユーザーフォームのコントロールを表示します。

このコントロールツールボックスから「テキストボックス」を選択し、ユーザーフォームをドラッグします。

これにより、このテキストボックスに関連付けられている多くのプロパティを確認できます。

TextBoxのプロパティ

F4キーを押して、テキストボックスのプロパティを表示します。

ご覧のとおり、テキストボックスには、このように名前、色、境界線があります。コーディング中にこのテキストボックスを簡単に参照できるように、固有名詞を付けます。

ユーザーフォームでTextBoxを使用する例

プロジェクトの1つをVBAテキストで実施します。従業員の詳細を保存するためのデータ入力ユーザーフォームを作成します。

ステップ1:ユーザーフォームにラベルを描画します。

手順2:ラベルのデフォルトテキストを「従業員名」に変更します。

ステップ3:ラベルの前にテキストボックスを描画します。

手順4:テキストボックスに「EmpNameTextBox」という適切な名前を付けます。

ステップ5:最初のラベルの下にもう1つのラベルを描画し、「従業員ID」としてテキストを入力します。

手順6: 2番目のラベルの前に、もう1つのテキストボックスを描画し、「EmpIDTextBox」という名前を付けます。

ステップ7:もう1つのラベルを描画し、テキストを「給与」として入力します。

手順8:「Salary」ラベルの前にもう1つのテキストボックスを描画し、「SalaryTextBox」という名前を付けます。

ステップ9:ツールボックスから「コマンドボタン」を挿入します。

手順10:コマンドボタンのテキストを「送信」に変更します。

これで、ユーザーフォームのデザイン部分は完了です。次に、このユーザーフォームに入力されたデータを保存するコードを記述する必要があります。現在、F5キーを押してユーザーフォームを実行すると、次のようなユーザーフォームが表示されます。

Step 11: Change the Caption of the User Form in the Properties window.

Step 12: Now double click on the Submit Command Button.As soon as you double click you will see this auto sub procedure like the below.

This is when you click on the Submit button what should happen. We need to mention the tasks in the VBA code. In this project, our aim is to store the data entered in the text box as soon as we click on the Submit Button.

For this first create a Template like this in the worksheet named “Employees Sheet”.

Step 13: Now come back to the visual basic editor. Inside the button click subroutine in VBA first determine the last used row by using the below code.

Code:

 Private Sub CommandButton1_Click() Dim LR As Long LR = Worksheets("Employee Sheet").cell(Rows.Count, 1).End(xlUp).Row + 1 End Sub 

Step 14: First thing is in the first column we will store Employee Name. So for this, we need to access the text box named as “EmpNameTextBox”.

Code:

 Private Sub CommandButton1_Click() Dim LR As Long LR = Worksheets("Employee Sheet").cell(Rows.Count, 1).End(xlUp).Row + 1 Ramge("A" & LR).Value = EmpNameTextBox.Value End Sub 

Step 15: In the second column we need to store Employee ID. So this will be obtained by accessing the text box named “EmpIDTextBox”.

Code:

 Private Sub CommandButton1_Click() Dim LR As Long LR = Worksheets("Employee Sheet").cell(Rows.Count, 1).End(xlUp).Row + 1 Ramge("A" & LR).Value = EmpNameTextBox.Value Ramge("B" & LR).Value = EmpIDTextBox.Value End Sub 

Step 16: At last we need to store the salary part, for this, we need to access to text box named “SalaryTextBox”.

Code:

 Private Sub CommandButton1_Click() Dim LR As Long LR = Worksheets("Employee Sheet").cell(Rows.Count, 1).End(xlUp).Row + 1 Ramge("A" & LR).Value = EmpNameTextBox.Value Ramge("B" & LR).Value = EmpIDTextBox.Value Range("C" & LR).Value = SalaryTextBox.Value End Sub 

Ok, we are done with the coding part as well. Now run the code using the F5 key we should see a User Form like the below.

As of now, all the boxes are empty.

Fill the details first.

Now click on the “Submit” button, it will store the data to the worksheet.

Like this, you can keep entering the data and hit and submit button. This is the simple data entry user form with a text box.