Step into professional development with Excel user form events

Professional Development
12 followers
7 Video Views·Sep 10, 2025
In this week’s training, we’ll explore how UserForm Events can transform your Excel applications. You’ll learn how to create dynamic UserForms that load customer data instantly, respond to actions like opening or closing, and even check to see if data is saved before closing. This session will cover key events like Initialize, Activate, and Deactivate, so you can keep your forms running smoothly and efficiently.
User forms are one of the most powerful tools in VBA and Excel.
But what about the events that run with user forms?
How do we use those events to our advantage when we create incredible applications?
Hi, this is Randy with Excel for Freelancers.
In today's VBA basic training,
we're going to go over everything you've ever wanted to know about user form events
and how you can use them in your applications to your advantage
to create very quick, very powerful applications in Excel.
So we're going to get right into it.
I do want to thank you for being here.
Of course, if you do like this channel, there are so many ways to support us.
I create these basic VBA trainings each and every weekend just for you.
And of course, the comprehensive application developments every single Tuesday.
Regardless of the type of training, this template, the workbook is absolutely free.
As always, all you need to do is just click the link below.
If you're on my website, you can look for a download button.
If you're on YouTube, just look for the word download.
I'll make sure to get this workbook to you.
All you need to do is enter your name and email.
Please don't forget to subscribe, sign up on our email so that I'll send you
these brand new trainings with free workbooks each and every week.
In fact, twice a week to do that.
Also, don't forget, I've got the 350 workbooks.
That means 350 of my absolute best templates.
And I put them all into a single zip file so you can get everything.
I've worked hard for for the last seven years into a single zip file.
That is also a great way to jumpstart your career, whether you're a freelancer or whether
you want to sell the workbooks or use them in your company.
I've got something for everyone inside that.
All right, let's get to this training.
What we're going to be doing is we're going to be creating a brand new user form.
And then I'm going to show you some of the powerful events inside that user form
so that you can use them to your advantage.
We've got a little bit of data here, the customer list here that's going to help us.
And I've got a cheat sheet.
So here are all the types of events.
We're going to be going over a few of the most popular ones.
But if you want to refer back to this, you always have this cheat sheet available
so you can understand how you can use these events, when you can use them,
and why you should use some of them.
All right, so we're going to get into it.
The first thing we need to do actually is to create a user form so that we can work with that.
Our user form is going to be based on this customer list.
So we certainly want to do that.
So to create your first user form, in fact, we might go a little quick on the user form
because I have dedicated trainings on the user form itself.
But this one, we're going to focus on the events.
But I still want to create a user form with you because I love to share everything with you.
Sometimes I do a lot of sharing hours at a time, even though you guys might sleep.
Let's get into it.
So what we want to do is we want to look for the developers tab.
If that's not available, you can right click anywhere and click customize the ribbon.
Just click this developer.
That'll ensure that the developers tab is visible.
It is inside the developer tab that you'll reach the visual basic.
If you're lazy, just like me, you can click Alt F11 from wherever you are.
And it's going to launch this VBA editor.
And you'll see a properties here, maybe or maybe not.
So we'll have just a blank editor.
So there's not much inside this editor just yet, but that's okay.
We are going to make it powerful and we're going to get to everything.
So I've got two sheets.
Sheets one is our customer, sheet two is our cheat sheet.
That's all I have.
We want to create a brand new user form.
Insert is a great way.
And we're going to click insert user form.
I want to make this bigger.
I want it to contain all the customers and maybe a customer list.
So I'll do something like that just to make it a little bit bigger.
That properties that you just saw that I closed, we need to see that again.
So we want to click the properties.
F4 is a shortcut there.
Now your properties might be pinned here, might be here.
It might be somewhere else.
Depends.
We can pin that wherever we want if we want to pin it, but I'll keep it right here close
to where we're working.
Let's just give this a name.
So customer form.
That's the name of the form.
That's not the caption up here.
So we're going to look at the caption and we're going to go into this caption right here.
And we're going to call this customer something very simple in capital letters.
And we'll try to spell that right for a change.
So now what we want to do is we want to create some fields and a list box.
So I've got a list box.
I want a list of customers.
So I'm going to use this.
So the list of customers is going to be like here.
And then what I want to do is I want to create a little bit of a header.
Just keep it simple.
So we're going to do this again.
A header here.
I want it centered because it's a header.
So I want to center inside there.
And I also want to center within the form itself.
So I'm going to click out, click in and center that in the form.
I want to make this a larger one.
So I'm going to select on the font here.
And we're just going to use something a little bit larger font.
We'll use 12 and bold and then click okay.
We don't need to change the name of the label.
But we certainly want to change the caption of it.
So we're just going to call this customers.
So great that we have that.
We can see that take effect.
Now we need some fields and some labels.
So what we want is we want our labels to pretty much match the order of this.
Customer name, address, city, state, zip, phone and email.
So that is what we want to do.
And I'm going to use the same order.
So what I'm going to do is I'm going to click here.
And you see this toolbox pop up.
But if you don't have it or yours disappears or it goes somewhere you don't know.
We can click the toolbox here and it'll reappear just like magic.
There's a shortcut here.
Although I have no idea what it is.
Hopefully somebody will tell me.
So I click there for toolbox and we're going to click on the label here.
So we're just going to put in our customer name.
Once we get one label formatted just the way we like it, we can then duplicate it.
So I'm going to put it on the, actually put it on the right here.
And what I'll do is I'll change the font.
The fonts are inherently a little bit small on a user form.
So we'll go with 11 on that.
And then I'm going to just basically put in our customer name.
That's fine for us.
Now what we want to do is we want to pretty much duplicate this label for each one of our fields.
So I'm going to control C and then control V.
This one will be for our address.
I'm going to use another one for our city here.
I'm going to paste another one for our state here.
And the zip code, I'm going to bring the zip code over here because I'm going to put both in there.
And I'm going to paste this one here.
This is going to be for our phone.
And then this is going to be for our email.
So I think I've got everything.
So now what we want to do is we can select inside here, as you saw me,
which would be a slow double click, slow.
If you do a quick double click, you're going to get into the code,
which we're going to get into.
So we want to do a slow click.
Another way to do that is to select on here and then go into the properties of that.
We did see the properties.
We don't know what happened to them.
They've gone in to another la la land.
So we'll select it on here.
And we can also select on here too, the caption too.
So there's two ways to do that.
We'll change this to address.
This is going to be our city.
Sometimes I like to do both.
So city.
This will be our state.
And then we just have our zip code.
So we can see quickly how to do that.
Zip code.
And then we also want here our phone number.
Actually, that's going to be the state and the zip code.
We'll make this the state and then the zip code.
And we need to use a longer name for both email and our phone number.
So this will be our zip.
Because we don't have a lot of space on that.
So zip.
And then lastly, we're going to put in the two, which is going to be our phone and email.
So we'll put this to phone and email.
We're going to create some buttons too.
We want one for, see that's a quick double click.
We need a little bit slower on that.
And email.
Very good.
So now that we have that, what I would like to do is just shrink this one up.
We don't need this one so big.
I also want to enter some fields.
Now we want to keep the font name the same.
The fields is going to be right here.
This is a text box for our field.
So we'll make that long, like about right here.
We'll set the height to about 18 on that.
It's sufficient.
We will want to make sure that it's left justified, which it is.
I also want to make sure the selection margin is false.
I don't need the space there on that margin.
We do want the font size.
So I'm going to select the font size.
And just as we did, we're going to use 11 as the font.
And that's going to look pretty good.
If we check the value just to do a test here, we can see that it looks good.
And so that's exactly what I want.
Great.
So that's going to be for our customers.
Now we're going to give this a name.
I'm going to call this field one.
So field one, very specific.
Actually, we'll do the names a little bit later, but that's okay.
Control C, Control V.
Now what I'm going to do is I'm just going to place these right here.
And we don't need to necessarily make sure everything lines up just the way it is,
but we can easily fix that.
So what I'm going to do is I'm just going to go in order.
And generally, if you go in order here, the tab stop will also be in order.
So that's just kind of a nice feature.
We can align things.
We're not here to make things perfect just yet.
And then the phone here, we're going to put that down here.
And then we're going to put the email here.
Now, if you quickly want to align it, all you need to do is just draw a line up like this.
And then we can make them on the left.
So let's bring this up here and align the left, which we can do here.
Oops, let's fix that.
Draw a line down here.
And then here we go.
We're going to align the lefts here.
That looks good.
And I'm going to align the rights here.
So we just select them and then align the rights.
And that's going to line everything up just nicely.
Very good.
So we want to do the same thing here.
I'm just going to draw a line here to capture them all and align the rights.
Also, we can change it here.
So if you want to move the left, I want to bring it over a little bit left.
So we can do 140.
That's going to line everything up quite nicely.
And the spacing can work.
We do want to add some buttons inside that.
So let's go ahead and just add some buttons.
So now what we're going to be doing is I'm going to select right on here.
It's called the command button.
And I'm going to make this one for, let's say, save or update.
So I'm going to select inside it.
That'll also change save or update.
Again, the font size on this, we're going to go to the font size.
And we're going to make that a little bit larger here.
So we're going to go in on 11 and bold here.
That looks good.
And we're going to copy that.
And I'm going to paste that here.
And we're just going to use this to add new.
Very good.
So now I just want to name the form.
So it's going to make things easier, although it's not pivotal for this.
We've already called this field one.
But what I'd like to do is I'm going to copy this field here, just the text.
And I make this field too.
Now it's going to make our coding a lot easier if I want to save the customers
or if I want to update or load them super easy inside the code.
That's why we're naming everything just like this.
So each individual field, we're going to say field.
And then this is one, two, four, try to keep five, six, and seven.
So everything's pretty much done.
After I add these field names, our field name is good to go.
So we're going to paste that in there.
And then email is going to, oops.
So I've already got that one.
So that's going to be, let's see.
This is one, two, three, four, five.
This is six.
My counting's new.
And this is seven.
All right, so we're good to go on the names.
Now our user form is essentially built.
So what I would like to do inside these, we're going to add some things that we want to do
when the user form loads, when these selected.
And when we do some things, we want to create some events.
So what are some of the events that I want to do?
Well, let's say I want to run this form and I want to populate this list,
this list on the left with a list of customers.
I want that to happen when the form opens.
Well, there's an event that would go into that.
And that happens on an event.
So let's take a look at some of those events.
To get to the events, what we're going to do is we're going to right click.
And I'm going to click view code.
And we're going to go into here.
These are all the information that we've added for a user form.
We're going to pay special attention to the user form.
And if we look on the right side here, all of these things are events.
These are things that happen during user form.
When a user scrolls, when a user opens, when a user clicks a user form,
when the form is activated, or when it's initialized.
And I'm going to explain the difference.
It took me a while to understand the difference between activate and initialize
and deactivate and close.
So we're going to go over that because they can be kind of confusing.
And they're very, very helpful to know them.
So the first one we're going to go over is the initialize event.
Now the initialize, let's just take a look at this message box initialize.
So we understand exactly when that happens.
So if I were to run this, and I just run it here,
we're going to see that it says initialize form.
So it's something that happens before we actually see the form.
The initialize will take effect before.
So if I select OK, the form opens up.
So it's something that happens before we see the form.
And that's exactly what I want.
So here in this particular training, I want to take this list box,
and I'm going to give it a name.
We're going to call it, let's say customer list.
And what I would like to do is I would like to populate this list box
with the list of customers.
Now we're going to use this parameter right here.
It's called row source.
But I want it to happen when the form initializes.
And what is the list?
I want to take a list of these customers names, and I want to populate it.
So the first thing we want to do is create a list of customers,
a dynamic list, so that as we add more customers, that list builds.
So it's going to be in the formulas.
And we're going to create a dynamic named range.
And I'm going to call it customer name.
So customer names.
I'm just going to copy that.
Control C.
And I'm going to go down here, and I'm going to write equals offset.
I'm going to start at the header.
We always do that because if there's no data, we won't have a problem.
Or if we delete all the rows, there won't be an error.
So I'm going to use comma.
And then I'm going to offset at one row down,
because our actual named range needs to start on row three.
So we're offsetting at one row down.
We're not going to offset at any columns over to the left or the right.
So I'm just going to leave that blank and write a comma.
Now I need to determine how many rows.
If I want to know how many rows, we're going to use the count a function.
So count a.
And then again, once again, I'm starting at the header.
And I'm going to go down a lot of rows.
So counting a, I want to know all the rows with text.
But I want to exclude that header.
So I'm going to use minus one.
How many columns?
Just give one column, that customer name.
So I've completed our formula.
And what I'm going to do is I'm going to tab out.
And I'm going to shift tab back in.
I'm going to make sure those dancing ants wrap around those customers.
Call customer name.
Click OK.
Great.
So now what we want to do is I want to use one of our events to populate that.
So once again, we're going to go back inside.
Let's pin.
There we go.
So that we don't have to keep messing with that.
So what I'm going to do is I'm going to view the code.
And I'm going to look at this initialize event.
Once again, we're selected the user form and our event.
Let's select it again, just so we know the user form and our initialize event.
So it's going to launch this.
So what I would like to do is focus on that.
So I'm going to use the M E.
That means it's our user form.
I'm going to look for the customer list.
That's that list box.
And what I want to do is I want to populate.
I want to use the property row source.
That means what that list box contains.
And it's going to equal what?
Our customer name.
So I'm going to paste that in there.
That's that named range.
And what that means is as soon as that user form initializes.
It is going to automatically show that.
Now we can write just a small macro here.
I want to write sub show customer form.
And all that's going to do is we're going to type in customer form.
That's the name or form FRM.
And then we can use dot show.
How did I know that was wrong?
FRM because I put the period and nothing happened.
See nothing happened.
So I know the name is wrong.
If I put in the correct name and I put the period.
It's going to show me that IntelliSense.
So I'm going to show it now.
I'm going to take this and I'm going to tie it to this button here.
I'm going to right click.
And what I'm going to do is I'm going to assign the macro.
And I'm going to paste it in there.
And it's going to be show customer form.
However, it's not showing up in there.
Why is that?
Why doesn't it show up?
It doesn't show up because it is inside our user form.
It's looking for code inside that.
So what we're going to do is we're going to insert a module.
And then inside this module is where I'm going to write that code.
So this code here is essential just for this user form.
So I'm going to copy that out and put it directly inside the module here.
Now if we right click here and we click assign macro.
We see that that macro is available.
So we can click it and it shows our user form.
And now we see that our user form is actually populated with the customers.
That initialized event populated.
The only thing we want to do, of course, is to change that font size.
To make it something a little bit better.
Consistent with our user form.
So we're going to go into the font here.
I'm going to set this to 11.
Going to click OK.
Now launching the user form.
We can also do directly from here.
It is still going to show the customers.
Which is perfect.
Now maybe we want a header on that.
We can also do that.
So inside our list box.
We see that we have an option here.
Inside the properties here of the customer list.
I want to make sure that that's a little bit bigger.
We're going to look in here says column heads.
I'm going to change that to true.
When I do that it's going to automatically display.
What's ever in the row above our list.
So now when I select on here.
We see it says customer name.
Very good.
So that's the initialized event.
Now we can do other things in the initialized event.
And I'm going to show you exactly how we do that.
But let's get into some of the other events.
That we can focus on.
So we're going to go back into our code here.
And I'm going to familiarize yourself.
Let's get rid of this.
And I am going to show you.
Some other events that we might want to use.
So let's say we have some code.
When the user form is terminated.
So I'm going to look down here terminated.
And I'm going to write in message.
Box this form has been closed.
So now what we're going to do.
Is we can run it from anywhere we want.
And then what we want to do.
Is we want to close it.
And we can see that we get the message box.
This form has been closed.
So terminate means the form has been closed.
So we have initialized.
Now some things that get a little bit confusing.
Is when you see activated and deactivated.
So that is what's a little bit confusing.
What I want to do is.
I'm going to click on customer form here.
I'm just going to change the show model to false.
And what that means.
Is that when I run this form.
I can also select some other things.
If it is set to true.
This form has been closed.
If it is set to true.
It will not allow me to do that.
So keep that in mind.
So if it's set to true.
And we run that user form.
You see we can't do.
Let's select on the form itself here.
And then you see we can't do anything else other than.
So it keeps the form at the forefront.
And sometimes we want that.
But this time I want to show you something.
So what I'm going to do.
Is I'm going to set that show model to false.
So let's go back in here.
Select the user form as well.
And we're going to go to show model.
We're going to set that back to false.
Now what I'm going to do.
So I'm going to create a brand new user form.
I can insert user form right here.
And it doesn't matter what's on this user form.
And once again with this user form.
I also want to set the show model.
I think it's pronounced.
God forbid I should mispronounce something.
That's typical in every video.
Great.
So we have this user form here.
And now what I would like to do is.
I want to run this.
So we see that we can have.
Let's cancel that.
Select inside the user form play.
So we see that we can have both user forms available.
So now let's focus on some of those two events.
We're going to focus on both activate and deactivate event.
Because there are times that we want to do both.
So here's what I'll do.
I'll first get rid of this annoying pop-up.
Because you've seen it.
And you know what it is.
So let's go ahead and comment that out.
I'll keep it in the code though.
So we're going to view the code.
And I'm just going to comment that out.
We understand that when terminating.
It's going to be closed.
So now what we're going to do.
Is I'm going to write small macro here.
Here it's going to be saying message box.
This form has been activated.
And I'm going to write another one.
That says it's been deactivated.
So we can understand the two events.
So we're going to go down here.
And we see that anytime we do write code.
They appear in bold.
So for the deactivation.
I'm just going to write here.
And then we'll write in.
So we've got that.
Saving our work so far.
And we're simply going to run this code.
So we see that it's been activated.
So we noticed that right away it got activated.
So if we use another user form.
Let's go ahead and take a look inside this form.
And I'm just going to simply run this form.
So now we can see we can run both forms.
This form has been activated.
Now watch this.
This form has been deactivated.
So as soon as I make this form active.
It deactivates this.
Thereby triggering the deactivation.
So it triggered the deactivation.
Once again I'm going to select it.
And it triggers the activation.
I select something else.
And it deactivates.
So we see how the active and deactive work.
I'm not closing the form.
So the terminating is something very different.
So I can close this.
Now when I close this.
It would also trigger that termination.
But we've already commented out.
So we can see how when we have two different user forms available.
That also triggers both the activate and deactivate.
So once again I'll comment these others out.
So we can see.
So let's go ahead and work on this initialize.
What I'd like to do is when I select a customer.
And I click edit the selected customer.
I would like all the customer information to populate in those fields.
So let's write a macro that's going to do that.
And it's going to be based on the active cell row.
The active cell row is the row that the user has selected.
So let's go ahead and write the macro.
And we're going to attach it to the initialize event.
So there's a few ways we can do that.
We can build it directly inside the initialize event.
So we can write all the macro in there.
Or we can write a separate macro.
And then we can attach it to the initialize event.
So let's do that.
Sub load customer.
So we're going to write that.
So now we're going to dimension our customer row as long.
We're also going to loop through our customer field.
So we certainly going to need the column.
Our customer column as long.
So our customer row is what?
Customer row is equal to the active cell dot row.
So that's the row that the user has already selected.
So now what we want to do is we want to focus on our customers.
So it's sheet one.
We didn't name it.
If we do, it's probably a good idea to do that.
Let's do that.
Let's change it from seat one to customers.
I'd like to do that.
So now we're going to focus on with customers.
And we want to make sure we've got the right sheet name, which we do.
If it's wrong, again, then tell us it's not going to appear.
So with our customers.
If, all right, dot range A and the customer row dot value equals empty.
Meaning there's no customer.
So it would be like, let's say we've selected this row or this row.
I want to give the user a message saying, hey, there's no customer on this row.
So we can do that.
Then message box there.
I'll just put no customer has been selected.
We can exit this up.
Exit sub.
So now assuming that they have selected the customer, we're going to write a loop for
our customer column is going to equal to one to what?
So our customer column is going to start out one and it's going to go all the way to,
and then we're going to say, what column is this?
We're not sure we can just write in column seven.
Now this is going to be helpful.
One, two, seven.
Why is that helpful?
Because also inside our customer form here, our fields are from field one, two, three,
all the way to field seven.
So that's going to work perfect.
So knowing that one, two, seven, always close our loop next customer call inside that we're
going to focus back on our user form.
So it's going to be our customer form dot controls.
Which control is it?
They all start with field and there's another number customer column.
So it's one through seven customer column dot value.
So the value of that control equals what?
Cells based on the customer row, based on the customer call.
Great.
So basically the idea is that I want to load all the customer details up.
You see how easy it is when we use our field labels like this, it's only three lines of
code.
Very, very simple.
So that's all we need to do to load in the customer details.
When do I want to run this?
I want to run this on the initialized event.
So I'm going to go back into the view, the code into the initialized event.
We've populated the list.
Now I'm simply going to paste in that and we're going to put in load selected customers.
Now we get more to that.
So what I can do is I can select on here and I can just edit the selected customers.
And you see now it's populated that I'm going to close this out.
I'm going to select on another one and I'm going to edit selected customers.
Very good.
So that will work just fine.
Now we can also do some other things.
We can have a sheet event.
Let's say I want to double click and open it up.
I can probably do that.
Let's go ahead and take a look and see how we would do that.
All I need to do is really show the form.
I'm going to go into the customer sheet.
I'm going to double click on that.
And I want to go into a worksheet event.
So I'm going to select on worksheet and then I'm going to select on before double click.
All I need to do is simply show the form customer form dot show.
Once it shows, it's going to run that initialized event.
So it's going to run this event initialize.
And it's going to automatically load this customer on initialize event.
So all I need to do now is just double click on here and it's going to load in Jackie.
If I select something else here.
So let's go ahead and close that here.
And we're going to select on Nancy and double click on it.
Nancy.
So we see how we can use sheet events to simply load in details.
Now what I would also like to do is I would like to know if the user decides to close this form.
How can I ensure that they have saved something?
There's another event and that basically before the close.
So we can run something called user form query close.
And what that's going to do.
It's going to ask the user a question before they actually close the form.
So we can have a query before we actually close it.
Let's go ahead and take a look how that might work.
So let's go ahead and go back into our customer form here.
And what we're going to do is we've got the user form.
We're going to look here.
Scroll down here.
And we want to look for query close.
That's the one we want.
So now we can write in something like if message box.
Are you sure you want to close this form before saving the customer?
Very good.
Now we can do something like VB yes or no.
And then what we can do is equals VB no.
And then we could just say oops.
Let's close this out.
Don't forget here.
We need the then.
Also what we want to do then is cancel equals true.
So if it's anything but that it's going to keep the user form open.
So let's take a look at how that would work once we're going to run it.
So we're going to run our user form.
We can just I don't really like the double click event.
I probably like the single click.
Why do I not necessarily love it?
Because once the double click is our cursors inside this cell.
So now I can't really do anything inside this form unless I exit out.
So I'm not a big fan of double click.
Maybe we can change that to selection change event.
So look how I've closed this form.
It says are you sure you want to close this form before saving the customer?
If I say no, it's nothing's going to happen.
If I say yes, then we can do something like save the customer.
So we can run that.
Although we haven't built that macro out.
Very good.
So why don't we do this now?
What I would like to do is I'd like to add a new customer and just launch the form.
That should be very easy.
Insert the module.
Here we're going to do is sub.
We can just use show customer form.
If I use that, but I don't want to load any customer, we can do that.
So let's write that just for the fun of it.
Sub.
Call this add new.
And all we need to do again is just simply show the customer form.
So we can do customer form dot show.
So it's exactly the same thing.
We would need both.
And again, once again, I'm going to copy this here and I'm going to right click.
But if we see here, I want to make sure that the customer form is empty and not necessarily
on any selected customer.
So we're going to assign the macro here and we're going to paste that in here.
Add new.
So if we take a look in here, we see no customer selected.
That's not really what we want.
We did launch the form, but what we want to do is we don't want that message box to come up.
So how can we avoid that?
Well, let's go ahead and take a look inside our code.
We see that we have the load customer, but what we're going to be doing is we only want
to load the customer when we edit the customer.
So we're going to change this to show customer, right?
So just show customer.
And this time we are going to use the load customer.
So we're going to change that to first loading the customer.
And inside this macro, we will display the customer form.
So we can do customer form dot show.
So that means if we go back into the customer form, view the code.
So we have the initialize event.
So when we initialize, we don't want to run this macro.
All we want to do is add the customer names.
So here now we see we can right click here, assign the macro to that's updated.
So here we're going to edit the selected customer, load customer and click OK.
So if I select on something and I click here, it's going to load that customer.
Perfect.
And then we can exit out of that.
Oh, I keep hitting no.
All right.
So we can exit out.
Now, if I want to add a new customer, it's going to be blank.
Even if we selected a customer.
Went a little bit fast on that.
So I want to make sure that I show you.
So inside here, when the user form initializes, all we're doing is populating the customer list.
We're not doing anything else.
When the user wants to add a new one, all we're going to do is show the customer form.
When the user wants to edit the selected customer and load the customer,
we're then going to load it up, populate it.
All right.
Now what we want to do is we want to save the customer.
So we can do that relatively easy.
Save customer.
I need to know, is it a new customer or is an existing customer?
But for now, we'll keep it very simple here.
All I'm going to do is just copy this here and I'm going to paste it down here.
Customer is the active cell row.
We know that if the customer and no customer has been selected for 127,
all I need to do is just reverse this.
And this time, what we're going to be doing is we're simply going to be taking whatever
is inside our user form and putting it into our database.
That's it.
And we don't need to show the customer form because it is already visible.
I'm going to then, we've created a customer.
If we take a look inside our view, the object, we have a button here.
Let's see if we've named it here.
Let's go ahead and give it a name because I prefer to give it a nice name.
We're going to call this save button.
And once again, we have the add new button.
So we already have that.
So button add new.
I probably would just change it to a cancel button.
We've already got the add new.
So let's go ahead and change this to cancel because I think we need to cancel.
And the cancel is simply going to be the unload.
So I like that a little better.
We already have it.
Perfect.
So save and update.
We're going to double click that.
That's going to go into our event.
That is a click event for the save button.
I'm going to paste that in here called save customer.
We've got another button here that we've just created called the cancel button.
All we need to do is simply unload the form or clear it.
Unload ME.
Since we're inside the code.
Now, if we're in another module, I would write in unload customer form.
So we can do both of those.
All right.
So we'll keep that in there.
Great.
So let's go ahead and take a look.
We'll edit the selected customer.
We're going to cancel that.
It's going to clear the form.
Do you want to save?
And we can click yes.
So that'll do it.
So now let's say we say yes.
Since we have the save customer, I'm going to copy this.
I'm going to go back into our form here inside the view code here.
And we're going to look at cancel equals true, right?
So if they cancel it.
Otherwise, if they say no, then we're going to paste the customer.
So here we're saving the customer.
If the cancel is not true.
So let's take a look at that.
So we're going to edit the selected customer.
We're going to close it.
And do we want to close it?
Yes.
So let's go ahead and edit that customer.
And we're going to now save it.
John Jamison.
Let's change the name here.
Save and update that.
And we see that his name has been updated.
The only thing we need to do is update this list.
Notice this list did not update.
Well, how can we do that?
Well, that's very easy with one line of code.
So we're going to go in here.
And we're going to then go back in our save macro here.
And we're going to just do something like this.
With our customer form dot customer list dot row source equals.
And what is the list is?
Is our customer name.
I think it's names or names.
If we're not sure, we just go into the formulas name manager.
And we see it's called customer names.
So we know we've got the right names.
So we're going to go back into the code here.
Write in names.
That is going to ensure that this list will update every time we save a customer.
So we're going to edit the selected customer.
And we're going to go back to John James here.
So we see now it's Jamison.
So we're going to go back to James.
And we're going to save and update.
So we see that it's updated.
So John Freders.
And now we click save and update.
The only thing you might want to do is you might want to close this form.
Which we could do on save.
I don't know.
That's up to you.
Now one more thing I'd like to do before I let you go.
When I select on a customer, I want that customer to load.
So how can we do that?
So let's go ahead and close this.
That's really annoying, isn't it?
So let's go ahead.
But I want to just comment that out.
But you at least you have that available.
So I'm going to view the code here.
And we're just going to comment this out for now.
Because we don't necessarily need that.
But we certainly understand its capabilities.
And certainly can be important.
Especially if your user is going to save some important information.
We want them to give that.
So we understand initialize.
And this one needs to be here for customer.
Very good.
So last thing what I want to do is when a user selects on something loading the customer.
We know that the active cell row is always going to load the customer.
So how can we do that?
What I'm going to do is I'm going to go into the customer.
I'm going to view the code.
Now we're going to focus on our list box.
Customer list.
When the user clicks, it's the click event.
Here's the click event.
I want something to happen.
Now what I want to do is if I just load it.
It's not necessarily going to load whatever customer.
So what I need to do is I need to select inside the sheet.
Whatever one they have selected.
What I mean by that is Betty White.
If they select it here.
I'm going to select Betty White directly inside A and 3.
So how do we do that?
Well when a user makes a selection.
Let me just cancel this out.
When a user selects on the click event.
I just want to do message box.
The item that they've selected is the list index.
So it's customer list dot list index.
So I'm going to just comment this out.
Just so you can see it.
Now what we're going to do is we're going to edit selected customer.
So this is 4.
This one is 0.
So we see that Betty White is 0.
But what I want to do is I want to take that 0.
And I want to actually select a 3 before I load it.
So how do I do that?
Well it's basically the list index plus 3.
That's the correct row.
So if they select Dolores list index is 1.
You see it's 1.
But I want to change that to row 4.
So how do I do that?
I'd simply add 3.
So that's exactly what we're going to do.
So we understand the customer list index.
So what we're going to do is customers dot range A.
And the customer list index plus 3 dot select.
So when we select it.
Then we load the customer.
It's automatically going to run.
The only other thing we might not want to do is we don't want to show the form.
If it's already shown that could create an error.
So let's take a quick look and see if it does.
And then we're going to run it.
And now what we're going to do is we're going to actually it looks perfect.
So you see no issues created.
Now when I make a selection.
Notice how it mimics the selection.
So when I select here it's going to select.
So that's a great way even though our cities and states are all the same.
But we see that the records are loading correctly for each one.
All right very very cool.
In this one we focused on user form events.
But we did cover a lot.
In fact I've got a cheat sheet full of events.
We did cover the activate.
We covered the deactivate.
We covered initialize.
We covered the user form query close.
So that was pretty cool.
So we covered a lot in this training.
I do appreciate it.
If you do like these trainings.
Please don't forget to subscribe.
Comment below if you're on YouTube.
I've got brand new updated exclusive trainings for Patreon members.
Along with PDF code books.
Workbook downloads.
And a whole lot going on Patreon.
So it'd be great to see you there.
Thank you so much for your continued support.
And we'll see you next week.
