ExcelVBAユーザーフォーム| インタラクティブユーザーフォームを作成する方法は?

ExcelVBAユーザーフォーム

VBAのユーザーフォームは、フォームの形式でユーザーからの入力を受け取るように作成されたカスタマイズされたユーザー定義フォームであり、テキストボックス、チェックボックスラベルなど、ユーザーが値を入力するようにガイドするさまざまなコントロールのセットがあります。ワークシートに値を格納し、ユーザーフォームのすべての部分に一意のコードがあります。

ユーザーフォームはExcelインターフェイス内のオブジェクトであり、このユーザーフォーム内で、ユーザーからデータを取得するための他の便利なカスタムダイアログボックスを作成できます。先輩が作成したマクロファイルやインターネットからダウンロードしたマクロファイルを操作しているときは、このようなユーザーフォームを見たことがあるはずです。

この記事では、ユーザーからのデータを保存するための同様のユーザーフォームを作成する方法を示します。

ユーザーフォームを作成する方法は?

同様に新しいモジュールを挿入する方法と同様に、ユーザーフォームを挿入するには、Visual BasicEditorの[挿入]ボタンをクリックする必要があります。

このVBAユーザーフォームExcelテンプレートはここからダウンロードできます–VBAユーザーフォームExcelテンプレート

これをクリックするとすぐに、ユーザーフォームも挿入されます。

これをプログラムする方法を説明する前に、このユーザーフォームをフォーマットする方法を説明しましょう。

ユーザーフォームのフォーマット

ユーザーフォームを選択してF4キーを押すと、プロパティウィンドウが表示されます。

このプロパティウィンドウを使用して、このユーザーフォームをフォーマットしたり、名前を付けたり、色や境界線のスタイルなどを変更したりできます。

このように、他のプロパティを試して、ユーザーフォームを確認してください。

次に、このユーザーフォームにツールボックスを挿入します。

これで、このようなツールボックスが表示されます。

この時点で、ユーザーフォームはプログラムされていない状態で挿入されています。実行ボタンをクリックするだけでどのように機能するかを知るには、Excelワークシートにフォームが表示されます。

ToolBoxの描画ラベルを使用しています。

ラベル内に従業員名としてテキストを入力します。

このラベルでは、プロパティを使用してフォーマットできます。これで、テキストを「従業員名:」として入力しました。これは、キャプションの下のプロパティウィンドウに表示されます。

もう1つのラベルを挿入します。もう1つのラベルを挿入するには、ツールボックスをクリックするか、Ctrlキーを押しながら現在のラベルをドラッグすると、現在のラベルのレプリカが作成されます。

これで、同じラベルが作成されます。

名前を従業員IDに変更します。

同様に、もう1つのラベルを挿入し、「Department」という名前を付けます。

ツールボックスからテキストボックスを挿入します。

Name this text box as EmpName in the properties window.

Like this insert two more text boxes in from of Employee ID & Department respectively. Name those text boxes as per their heading.

Similarly, do it for the Department.

Now from toolbox insert Command Button.

Change the Name of the Command Button to “SubmitButton” and change the caption to “Submit”.

Insert one more button and call it “Cancel”.

Now just to see run press the run button or use F5 key and see how your userform looks like in Excel.

Now it is coming to the shape.

VBA Code

Now the user will enter data in this, so we need to program this to store the data entered by the user on this form.

Double click on the SUBMIT button, it will take you to the macro window with an auto-created macro like the below.

It says SubmitButton click, remember we had given a name for SUBMIT button as SubmitButton.

So, whenever we want to call this button we can call this by this name (submit button). Inside this macro copy and paste the below code.

Code:

 Private Sub SubmitButton_Click() Dim LR As Long LR = Cells(Rows.Count, 1).End(xlUp).Row + 1 Cells(LR, 1).Value = EmpName.Value Cells(LR, 2).Value = EmpID.Value Cells(LR, 3).Value = Dept.Value EmpName.Value = "" EmpID.Value = "" Dept.Value = "" End Sub 

  • EmpName.Value here EmpName is the text box name we had given while creating the employee name text box.
  • EmpID.Value here EmpID is the text box name of Employee ID text box.
  • Dept.Value this is department text box name.

So, on the click on submit button it will store the values in the mentioned cells.

Now double click on Cancel button, this will also show you the auto macro name like this.

Copy the below code and paste.

Code:

 Private Sub CancelButton_Click() MyUserForm.Hide End Sub 

MyUserForm is the name we had given to the userform. MyUserForm.Hide means on the click on the CANCEL button it will hide the userform.

Ok, now create a template like this in the worksheet.

Delete all the other sheets in the workbook except this template sheet.

Now go to Visual Basic Editor.

And run the macro using F5 key or manually, we will see user form in front of us.

Enter the employee name, employee id, and Department name.

Now if you click on the SUBMIT button, it will store the values on to the template we have created.

Like this you can keep entering the names, userform will keep storing the values entered by the user in the specified cells.

So by using USER FORM, we can actually create wonderful projects to get the information from the user.