1. Macros
- Warning: As a precaution, you should make a backup copy of the file, before you remove any code.
- Right click on any sheet tab and choose View Code, to open the Visual Basic Editor.
- In the Project Explorer at the left of the screen, find the workbook. In the sample shown here, Book4 is the workbook name -- VBAProject (Book4)
- Look for a Modules folder, and open it. (If there is no Modules folder, go to Step 6.)
- For each module in the folder:
- Right-click on the module name.
- Choose Remove Module1 (the name of your module may be different)
- Click No when asked if you want to Export.
- Open the Microsoft Excel Objects folder.
- For each worksheet, and for ThisWorkbook:
- Double-click on the object name, to open its code module. In this sample, you'd double-click on Sheet1 (Sheet1)
- On the keyboard, press Ctrl+A to select all the code (even if the code module looks empty)
- Press the Delete key.
- Look for a Forms folder, and open it.
- Delete any UserForms that it contains.
- Look for a Class Modules folder, and open it.
- Delete any class modules that it contains.
- Close the Visual Basic Editor.
- Save the changes to the workbook.
A macro to remove all VBA code in a workbook is available
at Chip Pearson's web site:
http://www.cpearson.com/excel/vbe.htm
The next time you record a macro, the toolbar should automatically appear.
Note: When you're finished recording, click the Stop Recording button. If you close the toolbar by clicking the X, it will disappear again.
2. User Defined Functions
3. Buttons
4. Run Macros Automatically
How do I run a macro every time a certain cell changes its value?
There is an event called Worksheet_Change which is triggered when a value is entered (it will not fire when a formula result changes). One of the arguments to this event is 'Target' which is a reference to what changed. Since this event will occur whenever a value changes - you can use the target to see if it is the cell you are interested in:
Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("C5")) Is Nothing Then Exit Sub Else 'The cell you are monitoring has changed! 'Do whatever you need to do... End If End SubI want Excel to run this macro automatically every time the Excel file is opened.
![]()
Place the code in (or call it from) the Workbook_open event of the ThisWorkbook module in the VB editor. Or simply name your macro Auto_Open. If you choose to use both then Workbook__open will run before Auto_open.
Auto_open will not run if the workbook is opened by another macro, you must use the RunAutoMacros method. Contrary; Workbook_open will run if the workbook is opened by a macro, you must use Application.EnableEvents = False to prevent it.
I want to add a toolbar to my spreadsheet that when clicked, brings up Userform1.
Assign the toolbar button to this macro, which should be in a standard VBA module:
Sub ShowForm () Userform1.Show End SubI want to show a userform each time my file is opened.
Combine the two solutions above:
Private Sub Workbook_Open() UserForm1.Show End Subor
Sub Auto_open() UserForm1.Show End SubSee Chip Pearson's http://www.cpearson.com/excel/events.htm for detail and many more useful events.
Is it possible to call a macro from the condition true or false side of a worksheet formula? i.e. If(A2="OK",Run macro1,run macro2)
Basically, the answer is No. You can write functions in VBA that you can call from worksheet cells, but these functions can only return a value. They can't modify other cells or alter any part of the Excel environment. (You may be able to use a worksheet change event to call the macro.)
5. Macros and Security
6. Working with Ranges
How do I find the first empty cell in column A?
If ActiveSheet.UsedRange.Count < 2 Then MsgBox 1 Else MsgBox Columns("A:A").Find(What:="", LookAt:=xlWhole).Row End IfHow do I find the cell below the last entry in column A ?
MsgBox Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row(This will return 2 on an empty column A)
orSub FindLastCell() Dim LastCell As Range With ActiveSheet Set LastCell = .Cells(.Rows.Count, "A").End(xlUp) If IsEmpty(LastCell) Then 'do nothing Else Set LastCell = LastCell.Offset(1, 0) End If End With MsgBox LastCell.Row End SubHow do I find the last row in my spreadsheet?
MsgBox ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).RowNote: You may want to reset the last cellI want to loop through all selected cells and perform an operation on each of them.
Sub DoOnSelection() Dim oCell As Range For Each oCell In Selection oCell.Font.Bold = True Next End SubI want to loop through all worksheets and perform an operation on each of them (unprotecting or whatever).
Sub AllSheets() Dim ws As Worksheet For Each Ws In ActiveWorkbook.Worksheets MsgBox ws.Name Next End SubI want to loop through all workbooks in a folder and perform an operation on each of them.
Sub AllFolderFiles() Dim wb As Workbook Dim TheFile As String Dim MyPath As String MyPath = "C:\Temp" ChDir MyPath TheFile = Dir("*.xls") Do While TheFile <> "" Set wb = Workbooks.Open(MyPath & "\" & TheFile) MsgBox wb.FullName wb.Close TheFile = Dir Loop End Sub7. Working with Files
FAQs collected by Harald Staff, Excel MVP
Posted by Debra Dalgleish, Excel MVP
Excel Tips and Book List