This page contains sections of common sections of code, which you can learn from, copy straight into your VBA projects, or just to remind you of the syntax.
Contents:
- Application Settings and Methods
- Arrays
- Auto Filter
- Charts
- Cells and ranges
- Comments
- Data validation
- Files
- Folders / directories
- Form Controls
- Loops
- Named Ranges
- Pictures
- Pivot Tables
- PowerPoint
- Print settings
- Security / Protection
- Selection
- Shapes
- Tables
- UserForms
- Workbooks
- Worksheets
- Zip and Unzip files and folders
Application Settings and Methods
Convert centimeters inches and pixels to points
Manipulating and changing scroll bars
Run a macro from macro (from another workbook)
Arrays
Sorting an array alphabetically with VBA
Auto Filter
Cells and Ranges
Add a prefix or suffix to every cell in selection
Apportion a value across cells
Cell and Range VBA properties and actions</
Convert Merged cells to Centre Across Selection
Draw rectangles around selected cells
Charts
Comments
Insert, delete and control comments
Data Validation
Loop through each item in a Data Validation list with VBA
Files
Selecting a file using the File Dialog Box
Find out if a file is already open by you or another user
Loop through all the files in a folder (and sub folders)
Copy, move, rename, delete and confirm existence of files
Password protect an Excel file
Folders / Directories
Selecting a file using the File Dialog Box
Create, delete, rename and confirm existence of folders
Loop through all the files in a folder (and sub folders)
Form Controls
Loops
Loop through every worksheet or every workbook
Loop through all selected sheets
Loop through all the files in a folder (and sub folders)
Named Ranges
Named Ranges – VBA properties & actions
Save Excel sheets, charts and workbooks as PDF
Pictures
Insert, move, delete and control pictures
Pivot Tables
Excel VBA Code for Pivot Tables
PowerPoint
PowerPoint VBA Reference Library
Print settings
Control the print zoom settings
Security / Protection
Protect and unprotect worksheets
Protect and unprotect workbooks
Password protect an Excel file
Selection
Check what is currently selected in Excel
Shapes
Draw rectangles around selected cells
Tables
Control and manage Excel Tables
UserForms
Workbooks
Loop through every worksheet or every workbook
Workbook properties and actions
Protect and unprotect workbooks
Password protect an Excel file
Manipulating and changing scroll bars
Worksheets
Worksheet VBA properties and actions
Loop through every worksheet or every workbook
Loop through all selected sheets