Excel VBA Userform | Ako vytvoriť interaktívny formulár používateľa?

Excel VBA Userform

Userform in VBA are customized user-defined forms which are made to take input from a user in the form of a form, it has different sets of controls to add such as text boxs, checkboxes labels, etc to guide a user to input a value a uloží hodnotu do tabuľky, každá časť používateľského formulára má so sebou jedinečný kód.

Userform je objekt v rozhraní programu Excel a vo vnútri tohto formulára používateľa môžeme vytvoriť ďalšie užitočné vlastné dialógové okná, pomocou ktorých získame údaje od používateľa. Ak pracujete so súborom makier, ktorý vytvoril váš nadriadený alebo si ho môžete stiahnuť z internetu, musíte vidieť takýto užívateľský formulár.

V tomto článku vám ukážeme, ako vytvoriť podobný formulár používateľa na ukladanie údajov od používateľa.

Ako vytvoriť Userform?

Podobne ako pri vkladaní nového modulu musíte kliknutím na tlačidlo INSERT v editore jazyka Visual Basic vložiť Userform.

Túto šablónu VBA Userform Excel si môžete stiahnuť tu - Šablóna VBA Userform Excel

Len čo na to kliknete, vloží sa aj užívateľský formulár.

Než vám poviem, ako to naprogramovať, dovoľte mi ukázať vám, ako formátovať tento formulár používateľa.

Formátovanie užívateľského formulára

Výberom užívateľského formulára stlačte kláves F4, zobrazí sa vám okno vlastností.

Pomocou tohto okna s vlastnosťami môžeme tento užívateľský formát naformátovať, pomenovať, zmeniť farbu, štýl orámovania atď ...

Takto vyskúšajte ďalšie vlastnosti, aby ste získali prehľad o používateľskej forme.

Teraz pre tento formulár používateľa vložte Panel nástrojov.

Teraz uvidíme panel nástrojov, ako je tento.

V tejto chvíli je iba vložený užívateľský formulár, nie je naprogramovaný. Ak chcete vedieť, ako to funguje, jednoducho kliknite na tlačidlo spustenia, uvidíme formulár na pracovnom hárku programu Excel.

Teraz sa používa štítok DrawBox ToolBox.

Do štítka zadajte text ako meno zamestnanca.

Pre tento štítok teda môžeme formátovať pomocou vlastností. Teraz sme zadali text ako „Meno zamestnanca:“ Teraz to môžeme vidieť v okne vlastností v časti Titulky.

Vložte ešte jeden štítok. Ak chcete vložiť ďalší štítok, kliknite buď na panel nástrojov, alebo môžete súčasný štítok pretiahnuť podržaním klávesu Ctrl . Budete mať k dispozícii repliku aktuálneho štítka.

Teraz budeme mať rovnaké označenie.

Zmeňte názov na ID zamestnanca.

Teraz podobne vložte ešte jeden štítok a pomenujte ho ako „Oddelenie“.

Teraz z panela nástrojov vložte textové pole.

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.