Workbook Events - Workbook Before Close event

Excel VBA for Beginners Events in Excel VBA
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.84
List Price:  €129.98
You save:  €37.14
£79.48
List Price:  £111.28
You save:  £31.79
CA$136.74
List Price:  CA$191.45
You save:  CA$54.70
A$150.88
List Price:  A$211.24
You save:  A$60.36
S$135.02
List Price:  S$189.03
You save:  S$54.01
HK$781.40
List Price:  HK$1,093.99
You save:  HK$312.59
CHF 90.57
List Price:  CHF 126.80
You save:  CHF 36.23
NOK kr1,084.75
List Price:  NOK kr1,518.70
You save:  NOK kr433.94
DKK kr692.49
List Price:  DKK kr969.51
You save:  DKK kr277.02
NZ$166.11
List Price:  NZ$232.56
You save:  NZ$66.45
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,956.06
List Price:  ৳15,338.92
You save:  ৳4,382.86
₹8,347.75
List Price:  ₹11,687.19
You save:  ₹3,339.43
RM473.85
List Price:  RM663.41
You save:  RM189.56
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,772.29
List Price:  ₨38,882.31
You save:  ₨11,110.02
฿3,673.43
List Price:  ฿5,142.95
You save:  ฿1,469.52
₺3,227.91
List Price:  ₺4,519.21
You save:  ₺1,291.29
B$507.22
List Price:  B$710.14
You save:  B$202.91
R1,843.50
List Price:  R2,580.97
You save:  R737.47
Лв181.60
List Price:  Лв254.25
You save:  Лв72.65
₩135,529.28
List Price:  ₩189,746.42
You save:  ₩54,217.13
₪374.14
List Price:  ₪523.82
You save:  ₪149.67
₱5,714.50
List Price:  ₱8,000.53
You save:  ₱2,286.03
¥15,376.28
List Price:  ¥21,527.41
You save:  ¥6,151.12
MX$1,692.97
List Price:  MX$2,370.23
You save:  MX$677.25
QR364.12
List Price:  QR509.78
You save:  QR145.66
P1,360.02
List Price:  P1,904.08
You save:  P544.06
KSh13,423.65
List Price:  KSh18,793.65
You save:  KSh5,370
E£4,794.44
List Price:  E£6,712.40
You save:  E£1,917.96
ብር5,730.37
List Price:  ብር8,022.75
You save:  ብር2,292.37
Kz83,526.97
List Price:  Kz116,941.11
You save:  Kz33,414.13
CLP$94,107.58
List Price:  CLP$131,754.38
You save:  CLP$37,646.80
CN¥707.87
List Price:  CN¥991.05
You save:  CN¥283.18
RD$5,800.92
List Price:  RD$8,121.52
You save:  RD$2,320.60
DA13,449.71
List Price:  DA18,830.13
You save:  DA5,380.42
FJ$224.39
List Price:  FJ$314.16
You save:  FJ$89.76
Q775.96
List Price:  Q1,086.38
You save:  Q310.41
GY$20,884.44
List Price:  GY$29,239.05
You save:  GY$8,354.61
ISK kr13,954.60
List Price:  ISK kr19,537
You save:  ISK kr5,582.40
DH1,006.90
List Price:  DH1,409.70
You save:  DH402.80
L1,768.33
List Price:  L2,475.74
You save:  L707.40
ден5,716.08
List Price:  ден8,002.75
You save:  ден2,286.66
MOP$803.50
List Price:  MOP$1,124.94
You save:  MOP$321.43
N$1,847.92
List Price:  N$2,587.16
You save:  N$739.24
C$3,674.94
List Price:  C$5,145.06
You save:  C$1,470.12
रु13,329.02
List Price:  रु18,661.17
You save:  रु5,332.14
S/372.66
List Price:  S/521.73
You save:  S/149.07
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,692.78
List Price:  ZK3,770.01
You save:  ZK1,077.22
L461.79
List Price:  L646.53
You save:  L184.73
Kč2,323.07
List Price:  Kč3,252.39
You save:  Kč929.32
Ft36,167.88
List Price:  Ft50,636.48
You save:  Ft14,468.60
SEK kr1,082.52
List Price:  SEK kr1,515.58
You save:  SEK kr433.05
ARS$87,865.40
List Price:  ARS$123,015.07
You save:  ARS$35,149.67
Bs691.31
List Price:  Bs967.87
You save:  Bs276.55
COP$388,509.43
List Price:  COP$543,928.75
You save:  COP$155,419.31
₡51,021.71
List Price:  ₡71,432.44
You save:  ₡20,410.72
L2,466.06
List Price:  L3,452.59
You save:  L986.52
₲747,341.53
List Price:  ₲1,046,308.05
You save:  ₲298,966.51
$U3,819.97
List Price:  $U5,348.11
You save:  $U1,528.14
zł401.45
List Price:  zł562.05
You save:  zł160.59
Already have an account? Log In

Transcript

Okay, guys, so now we're going to discuss the workbook before close event. And as the name suggests, this before close event gets triggered whenever you try to close a workbook and it gets triggered before the workbook is closed. Okay, so actually whatever code you write in there will be executed before the actual closing of the workbook happens. And you can get it of course by going to your VBA editor, double clicking here on this workbook and selecting from this menu workbook and selecting before close from this menu up. So this is how you can get the before close event. Now, there is actually a parameter here called cancel.

And what this parameter does is that it allows you to actually cancel the closing of the workbook, so by default, its value is false. However, if we change its value to true if you set its value to true the The workbook is not going to close. Okay, so you can actually prank someone with that parameter. So let's see that. So I'm just going to set cancel equals true, okay, and then a message box, this workbook is not closing. Okay, so I'm just gonna try closing the workbook now and this code is going to be triggered when I tried to close the workbook.

So I'm just gonna close it here. And you can see here it messagebox this workbook is not closing, and the workbook did not close, okay? So by default, the value of cancel is false. However, if it is set to true in your procedure, or in whatever code you write in the workbook before close event, the workbook is not going to be closed. Okay, so this is the workbook before close event. We're gonna have another more practical example next.

Okay, guys, so now we're gonna have another example a more practical one. Actually for the workbook before close event. So this is actually the dashboard that I show as an example dashboard on my Excel dashboard scores. And you can see here that I've got a workbook before close event running on that dashboard. And what that does is that it calls a macro called show ribbon says to actually put Excel to the previous status of showing the ribbon, okay, because actually, the ribbon is hidden whenever this workbook is opened, and also the sheet dashboard is activated whenever this workbook is open. So I'm putting Excel back to its normal status of showing the ribbon and showing all the tools you know at the top.

Because what actually happens when this workbook is closed, and if if I don't show the ribbon or if I don't run the macro that is called show ribbon before closing the workbook is that the formula bar is not going to appear when we open Excel afterwards. So if we open even any Other workbook or any workbook in Excel or open a new Excel workbook, the formula bar is not going to show and, you know, despite the fact that it's very easy to show the formula bar by going to the View menu on the ribbon and just sticking the formula bar, some users might not know that. So if you're using a computer with other people as well, sharing the computer, you know, not having the formula bar showing will seem awkward, okay, so this is why I try to make it simple if somebody else is using a computer with me, okay, so I'm actually going to show you that practically.

So I'm actually gonna remove the show ribbon macro. This is a macro or a procedure that I've written in another module, I'm just calling it inside the workbook before close event. Okay, so I'm just going to remove it here, and I'm going to save my workbook and this way now the ribbon is hidden, and actually not the whole ribbon will be hidden when we open a new workbook, just the formula bar. So I'm just going to close here, the VBA editor, I'm going to close this workbook and save it Okay, now, I've opened a new workbook a blank workbook actually. And as you can see here, the ribbon has disappeared because I have not run the show ribbon macro. So you can actually show the formula bar again by clicking on the View menu on the ribbon here and checking the formula bar and then you can see the formula bar here, but actually, the formula bar has disappeared because I had not run the show ribbon macro.

So this is a practical example we're actually having the show ribbon macro run on the before close event can make Excel act normally or show the formula bar so as to not have things seem awkward for other users or so who are not very good with Excel are not familiar with this option of viewing the formula bar using the View menu here on the ribbon. Next we will see another example as well. Okay, so another thing that I use the workbook before close event for is to actually automate my work. So maybe if I'm building a dashboard, if I'm building a dashboard, and I'm using a lot of tabs in my dashboard here, so there's a row data there is the dashboard. And This stop is the one that should show to the customer. And this is the interface of the dashboard.

And there's an analysis tab. And there could be another tab here called lists. And sometimes I might build a dashboard that has lots of tabs, maybe six or seven. And I just want to close my workbook. And you know, normally the the other tabs except for the Dashboard tab should be hidden, and I just don't want to hide them manually, like right click and click on hide. I just want them to be hidden once they close the workbook.

Okay, so we're gonna see how to do that. So I use this a lot actually. So here on the before close workbook event, in addition to the coal show ribbon line here, which is actually going to show the ribbon and make things nice awkward for other Excel users on might write actually a procedure to hide my worksheets so as not to need to hide them manually before I close my workbook. Okay, I'm not hiding them in this case here because this is an example dashboard that I showed to the students so I need everything to be shown for them. But normally in real life, I would actually hide the other worksheets except for the dashboard worksheet, okay, so any other worksheets are using the background and they are considered the backstage of this dashboard, I would hide them. Okay, so I'm actually going to dim Ws work sheet, so I'm going to actually declare a worksheet object variable and I can actually say for each Ws in this workbook dot worksheets, next Ws so I can actually write some code to hide the worksheets except for the dashboard worksheet.

So if Ws Name is not equal to dashboard, then Ws dot visible equals excel sheet hidden and if so, this actually will hide my worksheet if its name is not dashboard. Okay, so let's try that. I'm just going to try here closing the workbook. Okay, so let's say I've been working on this workbook and I open all the tabs because I needed to do some maintenance on this dashboard, maybe add more data or do some amendments or whatever. Okay, and I'm just going to close my workbook here. So as you can see here, Before closing, all the worksheets have been hidden except for the dashboard worksheet.

Okay, so I'm gonna save it here. Let's say I'm gonna save it. Now I've reopened it again, as you can see here, all the other worksheets except for the dashboard worksheet are actually hidden right now. So if I need to start working again, I can unhide them, I can actually create another procedure as well just a normal procedure without any event that I would run, that would actually unhide all my worksheets if I want to start working and doing maintenance, so I can create another procedure that would actually unhide all my worksheets, right? So you can do that, I believe you can do that. I want you to attempt to do that on your own.

Okay, it's just gonna be a simple for each loop, looping through all the worksheets and unhiding them. And even if the worksheet is unhidden nothing's gonna happen. It's just gonna stay stay on hidden or visible. Okay, so I want you to attempt to do that. So that was the before close event. Thank you very much, guys for watching this video.

I'll see you on the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.