Enable macros in Excel and write safe VBA
Excel does not simply run macros when you open a file. That keeps malware out, but it is confusing when your own macro suddenly will not start. This lesson explains what each warning means and how to allow your own files. In the second part you build macros that ask before deleting and make a backup first.
In short: macros need a macro-enabled workbook (.xlsm). When you open one, Excel shows a Security Warning bar with an Enable Content button. Files from the internet or from email attachments are blocked completely on Windows; you unblock them in the file's properties. In your code: ask before deleting with MsgBox(…, vbYesNo), back up first, and check that a sheet exists before you touch it.
What you need to know: MsgBox and InputBox, worksheets and error handling.
Try it right now
A macro that deletes data should ask first. The exercise plays back your answer: Run clicks Yes, and Check also tries No.
Task The macro ClearList should delete the finished tasks, but only after asking. It counts the entries below the header and asks with Yes/No "Delete all <count> entries?", for example "Delete all 7 entries?". On Yes it clears A2 to column B of the last row (the header stays); on No it shows "Nothing was deleted." and changes nothing. Check answers Yes once and No once.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Task | Owner | ||
| 2 | Submit March expenses | Emma | ||
| 3 | Order printer ink | Josh | ||
| 4 | Update holiday planner | Priya | ||
| 5 | Review Carter quote | Josh | ||
| 6 | Close petty cash book | Emma | ||
| 7 | Collect storeroom keys | Liam | ||
| 8 | Print phone list | Priya |
Why Excel blocks macros
A macro can do anything you can do in Excel and more: create files, start programs, send data. That is why attackers use Office files with macros to spread malware, usually as email attachments. So Microsoft only runs macros once you trust the file. You do not need macros to read or edit a workbook; you only need them to run the automation inside it.
In real Excel: what each warning means
| What you see | What it means | What to do |
|---|---|---|
| Yellow Security Warning bar with Enable Content | The file contains macros, and they are off for now. This is the default setting, Disable VBA macros with notification. | Click Enable Content only for your own files or ones you know are safe. The file then counts as a trusted document, and its macros also run the next time you open it. |
| Red Security Risk bar: Microsoft has blocked macros from running because the source of the file is untrusted | The file came from the internet or an email attachment, and Windows marked it when it was saved (Mark of the Web). There is no button to enable macros here. | Only if you trust the source: close Excel, right-click the file in File Explorer, choose Properties, and on the General tab tick Unblock at the bottom, then OK. Open the file again. |
| A note that your administrator has restricted macros | Your organization blocks macros by policy. | You cannot change that yourself. Ask IT whether the file can be signed or stored in an approved location. |
Trusted locations
If you work with your own macro files all the time, you can mark a folder as trusted. Excel opens files in it with macros enabled, including files from the internet that would otherwise be blocked. Microsoft's steps: File > Options > Trust Center > Trust Center Settings > Trusted Locations > Add new location, then pick the folder with Browse. Only add folders where nothing from outside ends up. Your Downloads folder never qualifies.
Macro settings and file format
The general setting lives under File > Options > Trust Center > Trust Center Settings > Macro Settings. The choices are Disable VBA macros without notification, Disable VBA macros with notification, Disable VBA macros except digitally signed macros, and Enable VBA macros. Microsoft explicitly does not recommend the last one, because any code then runs without asking. On managed work computers, IT can lock these settings.
Excel only keeps macros in macro-enabled workbooks, so save as .xlsm. If you save as .xlsx, Excel warns you and leaves the macros out. How to add and run a macro is covered in VBA basics.
Excel on the web and on a Mac
In Excel for the web you cannot create, run or edit VBA macros. The macros stay in the workbook, and Open in Desktop App takes you to the desktop version to work with them. According to Microsoft, the block on files from the internet applies to Office on Windows, not to Office on a Mac.
Writing safe macros: three rules
You cannot undo a macro with Ctrl+Z. Once something is deleted, it is gone, apart from the last saved file. Three habits keep you out of trouble:
- Ask before deleting:
If MsgBox("Delete all 12 entries?", vbYesNo + vbQuestion) = vbNo Then Exit Sub. The number helps: if it is much bigger than expected, something is off. - Back up before changing:
Worksheets("Prices").Copy After:=Worksheets("Prices")puts a copy of the whole sheet right behind it. The copy becomes the active sheet, soActiveSheet.Name = "Backup"renames it. - Check before you touch: loop over
Worksheetsto see whether a sheet exists before you delete or use it. Otherwise the macro stops with run-time error 9.
Dim answer As Long
answer = MsgBox("Delete all " & entries & " entries?", vbYesNo + vbQuestion, "Clear list")
If answer = vbYes Then
Range("A2:B" & lastRow).ClearContents
Else
MsgBox "Nothing was deleted."
End If
| Constant | Value | Meaning |
|---|---|---|
vbYesNo | 4 | Yes and No buttons |
vbQuestion | 32 | Question mark icon, added with + |
vbYes | 6 | Returned when Yes was clicked |
vbNo | 7 | Returned when No was clicked |
Another exercise: back up before a price rise
Before the macro changes any price, it copies the sheet. Check looks at the sheet order, the name of the copy and the prices on both sheets.
Task The macro RaisePrices should raise every price in column B by the percentage in E1. Before that, it makes a backup copy of the Prices sheet: directly after Prices, named Backup. The copy keeps the old prices.
Prices
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Item | Price | Increase | 3% | |
| 2 | Copy paper | 4.20 | |||
| 3 | Black toner | 49.90 | |||
| 4 | Ring binder | 2.15 | |||
| 5 | Ballpoint pen | 0.85 | |||
| 6 | Sticky notes | 1.60 | |||
| 7 | Hole punch | 7.40 |
Suppliers
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Supplier | |||
| 2 | Shaw Paper | |||
| 3 | Office Supply Co | |||
| 4 | ||||
| 5 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
One more: delete a sheet only if it exists
The sheet name comes from a cell. Check also tries a name that is not in the workbook.
Task The macro DeleteSheet should delete the sheet whose name is in B1 on Start, but only if such a sheet exists. If it does not, it shows "There is no sheet called <name>.", for example "There is no sheet called Archive 2023." Excel should not ask for confirmation when deleting. Check also tests a name that does not exist.
Start
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Sheet to delete: | Archive 2024 | ||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Data
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Month | |||
| 2 | January | 48,250.00 | ||
| 3 | February | 51,730.00 | ||
| 4 | ||||
| 5 |
Archive 2024
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Year-end 2024 | |||
| 2 | Sales | 512,400.00 | ||
| 3 | ||||
| 4 | ||||
| 5 |
Archive 2025
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Year-end 2025 | |||
| 2 | Sales | 538,900.00 | ||
| 3 | ||||
| 4 | ||||
| 5 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
When a sheet is deleted, Excel normally asks for confirmation itself. Application.DisplayAlerts = False turns that prompt off. Turn alerts back on with = True right afterwards, and ask with your own MsgBox if the sheet might hold anything important.
Common mistakes
Ignoring the answer
MsgBox "… delete?", vbYesNo without parentheses or a variable shows the question, but the macro deletes either way. That works when the user clicks Yes and goes wrong on No. The exercise then says your code works on the visible data but not on the other test data. Always keep the answer in a variable or test it directly in the If.
Changing the backup instead of the original
After Copy, the copy is the active sheet. If you then write Cells(i, 2) without a sheet, you change the backup. Check reports that Prices!B2 still shows 4.20 where 4.33 was expected. Work through a variable instead, as in prices.Cells(i, 2).
Not renaming the copy
Without ActiveSheet.Name = "Backup", the copy is called "Prices (2)". Check says "The sheets are not right. Expected: Prices, Backup, Suppliers. Yours: Prices, Prices (2), Suppliers."
Deleting without checking
Worksheets(sheetName).Delete on its own works as long as the sheet is there. After a typo, or when the sheet is already gone, you get "Run-time error '9': Subscript out of range".
FAQ
Excel says macros have been disabled. What now?
That is the default: Excel opens files with macros with the macros switched off at first. With a yellow bar, click Enable Content if you trust the file. With a red bar, the file from the internet is blocked, and only Unblock in the file properties helps. The table above shows which bar means what.
Why is there no Enable Content button?
The red Security Risk bar leaves it out on purpose: the file came from the internet or an attachment. Unblock it in the file's properties only if you trust where it came from. If you do not know what the macros do, delete the file instead.
Should I just choose Enable VBA macros?
No. Macros in other people's files would then run without asking. Keep the default and put your own macro files in a trusted location.
Are .xlsm files dangerous?
An .xlsm file can contain macros, an .xlsx file cannot. The risk is not the format but code from an unknown source. You can always read your own macros in the VBA editor; with someone else's file, look at the code before you enable it.
Do I need to enable anything for the exercises here?
No. The exercises do not run in Excel but in a simulated workbook on our server. Your code cannot read files or start programs there.