You are on page 1of 9
2oraisit Data Entry using UserForm - Excel VBA Form Contals Home » Data Analysis » Data Entry using UserForm and VBA Data Entry using UserForm and VBA July 27, 2017 by excel365, Industrial Steam Boiler - Select the Right Sol Famous Steam Boiler Manufacturer Direct Sal, Ful Oil Gas Coal Biomass Wo boilercom/steamboile In today’s example we'll introduce data entry with the help of the UserForm. Using UserForm can considerably improve the interaction with our users. Furthermore this way we can assist them to execute particular steps They play a very important role in the automatization of Excel. In these days none of the business dashboards can be imagined without Form Controls. Let's say a few words about today's task. At times data entry can be very boring and by this can incline users to mistakes. We'll design a form that based on three data (Name, Age, Job title) will upload a “database’. Exactly as you can see on the picture below! What can exactly be seen on the picture? Three label, three text fields. And another thing, the Command Button. We'll assign to this a simple macro that will store input data. (rey otey | Iitps:llexceldashboardschool.com/userform-data-entry-vba 1/9 2oraisits Data Entry using UserForm - Excel VBA Form Conitals = Menu Data-entry Form Q How to create a UserForm to aid data entry? Let's start! The first step is to enter the Visual Basic Editor form the ribbon or by pressing the Alt+F11 buttons. @A-e-= user form-dato-en INSERT PAGELAYOUT FORMULAS DATA REVIEW Re) DEVELOPER FR\Map Properties | EE View Code Pi bpansion Packs COM | Ince Design A Micro ins | Meas RunDiog Ress Dat cote ns Cents va Vt as -F19) F OpetteVuttnic ation — El Open the VBA Editor. If the Project Explorer is not visible, click View, Project Explorer. Click Insert, and select the Userform. © Bile Edit Wiew | Insert | Formet Debug Bun Tools Add-Ins Window Help: Prot uaiesws @ After this place the fields and their names onto this form, Now insert the names of the fields by clicking on the Label Control button: + Fatmat Debug fun Toole Add-ins indaw Help Ribbon Commander wae alc bu aM Se wRlO p DataEntryForm emenrongyings seote fo {No [ Pivacy poe} Iitpslexceldashboardschool.comiuserform-data-entry-vba 2/9 2oraisits Data Entry using UserForm - Excel VBA Form Contals = Menu Change any elements and attributes of our form by right-clicking on them and then choose the Properties window. ‘As next step we have the option for example to set the fonts, size and colors. frst Fant Deg Reh Toke SEE Wed Hep bon Commands, rs AA) Cl> o alee wll xl Toobox El Sanenteen oN | Blsrectiche |e A ab | Himmiote gy @ gt ‘We can be all creative regarding the designs. We can make any kind we'd like or what we need for a given task. Now we will place another button on this form. The purpose of this is to fix the given data on one Excel table. We do this by inserting the CommandButton from the Controls: - VEAProject x | r a Toolbox senabete | staid | [pena (Comerutont a ccalerstor {No [ Pivacy poe} Iiitpslexceldashboardschool.comiuserform-data-entry-vba 3/9 2oraisits Data Entry using UserForm - Excel VBA Form Contals Name Age Add to list Job The user interface is ready! Data input will be a lot easier from now on. But we still have one very important thing to do. Because for the next step we have to determine the code behind it. In this example this would run by clicking on the “Add to list” button. Let's right click the CommandButton. From the drop down menu choose the View Code command. In the next chapter we'll show how to assign a short VBA code to this. ing Forward Send Bachward Adding Logic to Button in a Form Let's clear what do we expect from the code after clicking on the button? ‘Our expectation will be that it inserts the values of the UserForm into the determined cells of the determined {mo [ Prvacy poe} Iitpslexceldashboardschool.com/userform-data-entry-vba 4/9 2oraisits Data Entry using UserForm - Excel VBA Form Contals The first one with Integer type by the name rw to determine the current, still empty rows. The second one by the name ws and with type Worksheet. This will determine which worksheet our form will refresh / update the cells. We can already set the calues of the latter variable onto Sheet1 [commanasutont =] [ever Private Sob Commandmuccont_cliex() Dam ry As Integer Ser ws Worksneers|"sheeti") Use the Range Find method to determine the starting value of our nw variable. In our case we have to use the ws.Cells.Find method, because we are looking amongst the cells of Sheet! that contains anything (What We search by rows (SearchOrder (Lookin:=xIValues). [Previous) value Rows) for the row containing the last (SearchDirectior Than we add one to this number to find the first empty row: rw = ws.Cells.Find(What:="*", SearchOrder:=xIRows, SearchDirection:=xIPrevious, Lookin:=xIValues).Row + 1 ‘And from this point on our task is basically easy. The written value in the field must be equal with the rw variable, We store the number of the cells in the rw variable. We can find out the names of our fields from the UserForm Properties window in case we did not use standard names for them, Therefore, ws.Cells(rw, 1).Value (this is the cell of Sheet! first empty row in the first column) be equal to Me.TextBox1.Value which is the value was given into our first field. Me always refers to the parent object that we "have" our code. In this case it refers to UserForm, but if we was to write our code on a Sheet, that it would refer to that. Create User-Feedback MessageBox Here might be subservient to inform the user what happened to the input data, so with the help of the MsgBox command we can write a massage out for him, OK |) | No | [Privacy pot Initpsllexceldashboardschool.com/userform-data-entry-vba 5/9 zara Data Entry using UserForm - Excel VBA Form Controls appropriate cells The solution is easy: We make the value of the Me.TextBox1.Value equal to nothing. After this is useful to bring back the cursor to the first field with the help of the SetFocus parameter. a ee fe teretitscersaemero==-, Error handling — How can we do this? Basically we have to endeavor to write the codes with minimal possible errors. For this itis useful to insert an “If" branching so in case of empty cells the UserForm will do nothing. Our goal is to only store the data if all the cells / fields are filled out! The following short code will alert the user for insufficient data Simply turn our thoughts to the code, namely 1. Put (Wf after the removal of spaces (Trim) 2. Values of the first fields (Me TextBox1 Value) 3. Equal to nothing (= ") 4.then (Then) 5. write a massage (MsgBox “Please fll the field"), 6. then exit (Exit Sub) 7.and with this we can close the “If" branching (End If. The VBA code: OK |) | No | [Privacy pot Iitpsllexceldashboardschool.com/userform-data-entry-vba 6/9 2oraisits Data Entry using UserForm - Excel VBA Form Contals rw = ws. CeLls.tind(unat *, SearchUrder:=xilions, SearchDirection: 7 | If Trim(Ne.TextBox1.Value) = “* Then Wsghox "Please fill the Nane field!” exit Sub End If 1] Tf Trim(Me.TextBox2.Value) = "* Th Vsgtox "Please ill the age field! Exit Sub End IF If Trim(Me.Textfiox3.Value) = "* Then VsgBox “Please Fill the Job title field!” 7 | Exit Sub End IF ws.Cel1s(r, 1).Value = Me.TextBoxd.Value 1] ws.Cells(rw, 2) Value = Me. TextBox2. Value Ws Cells(rw) 2) Value = Me. TextBow Value Pe, TextBoxl /SetFocus, End Sub What else is there left to do? To determine how to start the UserForm, Here we can set events or key-combinations also. In our example we choose the simplest solution. We assign the startup of the code to a CommandButton. Under the Ribbon Developer tab the Controls section can be found. Let's insert a button with the help of the Insert command: Uss DATA RevW ew aC oeveLover | FoRMAT be Elerate a EA View Code Design sours © Document Mone, Aun Disiog itech Oat me vo esty vical S BoaQer. Data-entry Form By double clicking on the button in Desingn Mode we get over to the Visusal Basic Editor. We only need to show the UserForm named by us DataEntryForm command for the “Click” event. Here is the code snippet: Sub Button?_Click() 2 | DatatneryForm.Shon OK |) | No | [Privacy pot Iitp:lexceldashboardschool.com/userform-data-entry-vba 7/9 2oraisits Data Entry using UserForm - Excel VBA Form Contals John Doe a4 Manager Data-entry Form Q Conclusion: ‘Our today’s data input showcase example was a very simplified task. But it was perfect or us to demonstrate: It is worth to use Excel for serious tasks and the VBA help us to create interactive form for excel dashboard. We hope you got into the mode for VBA programming. We recommend that everyone start building their own little project! Download the VBA example! Related articles: ‘© Great macro collection to learn vba from scratch! + Create dynamic VBA presentation x Data Analysis Ultimate Dashboard Tools for Excel [ mo) [Piney oa ] Iiitpsllexceldashboardschool.comiuserform-data-entry-vba 8/9 2oraisits Data Entry using UserForm - Excel VBA Form Contals oK ) [No | [ Privacy pty | Iitpsllexceldashboardschool.com/userform-data-entry-vba 9/9

You might also like