VBA & macros
Automate repeat work with macros and VBA code.
- Excel VBA: How to Find the Last Used Column in a RowIn Excel VBA, find the last used column in a row by starting at the worksheet’s final column and moving left: ws.Cells(rowNum,…
- Excel VBA Find Last Populated Row: Reliable MethodsIn Excel VBA, use ws.Cells(ws.Rows.Count, "A").End(xlUp).Row to find the last nonempty cell in column A. If you need the last populated row…
- Excel VBA Filter Rows Containing Specific TextTo show only rows whose cells contain particular text, use Excel VBA’s AutoFilter method with a wildcard criterion such as *urgent*. For…
- Excel VBA Filter Multiple Criteria: Examples and CodeTo filter by multiple criteria in Excel VBA, use the range’s AutoFilter method: apply one criterion per field for conditions that must…
- Excel VBA File Open Error: Causes and FixesAn Excel VBA file-open error usually means Excel cannot access the file at the path supplied to Workbooks.Open, or it cannot read…
- Excel VBA: Fill a Formula Down to the Last Data RowTo fill a formula in B2 down through the last row that has data in column A, find that row with End(xlUp)…
- Excel VBA Subscript Out of Range: Causes and FixesExcel VBA “Run-time error 9: Subscript out of range” means your code asked for an item that does not exist at the…
- Excel VBA Export Chart: Image, PDF, and OptionsTo export an embedded Excel chart with VBA as a PNG, call its Chart.Export method and specify a file name and image…
- Excel VBA Sub or Function Not Defined? Fix It“Sub or Function not defined” is a VBA compile error: Excel cannot resolve the name of a procedure or function used by…
- Excel VBA Error Messages: Causes, Codes, and FixesAn Excel VBA error message means the macro could not compile, run an instruction, or complete an operation as written. Read the…
- Excel VBA Error Message Box: Show and Handle ErrorsAn Excel VBA error message box is usually created with the MsgBox function; use On Error GoTo to catch a runtime error…
- Excel VBA Error Numbers: Common Codes, Causes, and Fixes ExplainedExcel VBA error numbers are numeric identifiers that help you diagnose a problem when a macro fails; the number and its description…
- Excel VBA Error Line Number: How to Find ItExcel VBA does not automatically show a source line number in a runtime error message. To identify the failing line in code,…
- Excel VBA Error: Constant Expression Required ExplainedIn Excel VBA, “Constant expression required” is a compile error: VBA found a value that must be known when the code is…
- Excel VBA Error Handling: On Error, Examples, and Best PracticesExcel VBA error handling lets a macro respond to runtime problems instead of stopping unexpectedly. Use On Error GoTo Handler for a…
- Excel VBA Error 91: Why an Object Variable Is Not SetExcel VBA Run-time error 91 means your code tried to use an object variable that does not currently refer to an object.…
- Excel VBA Error Codes: Meanings and FixesExcel VBA error codes identify problems that stop a macro or signal that an operation failed. The most useful starting point is…
- Excel VBA Error 9: Subscript Out of RangeExcel VBA run-time error 9, “Subscript out of range,” means your code requested an item that does not exist at the position…
- Excel VBA Open File Dialog: Code and OptionsIn Excel VBA for Windows, use Application.FileDialog(msoFileDialogFilePicker) to display a file picker, let the user choose a file, and then open it…
- Excel VBA: Disable the X Close Button?Yes—Excel VBA can stop a workbook from closing when someone clicks its X by handling the Workbook_BeforeClose event and setting Cancel to…
- Excel VBA Error 1004: How to Check, Trace, and Fix ItTo check Excel VBA error 1004, click Debug when the error appears: Excel usually highlights the statement that failed. Then inspect the…
- Excel VBA: Copy a Formula Down to the Last Used RowTo copy a formula in Excel VBA down to the last row of data, find the last used row in a reliable…
- Excel VBA Copy Until Last Row: Reliable Code ExamplesTo copy a range in Excel VBA through the last populated row, find that row with End(xlUp), build a range ending there,…
- Excel VBA: Determine a Cell’s Background Color ValueIn Excel VBA, read a cell’s fill color with Range.Interior.Color; for example, Worksheets("Sheet1").Range("B2").Interior.Color returns its color as a numeric value. If conditional…
- Excel VBA Compile Error in Hidden Module: How to FixAn Excel VBA compile error in a hidden module usually means that code in a workbook, add-in, or other loaded VBA project…
- Excel VBA Label Not Defined: Fix the Compile ErrorExcel VBA’s Label not defined compile error means a procedure refers to a jump target that VBA cannot find. Check each GoTo,…
- Excel VBA Compile Error: Object Required ExplainedIn Excel VBA, the compile error “Object required” usually means a statement expects an object but is being given a value such…
- Excel VBA Color Codes: RGB, Hex, and ColorIndex GuideIn Excel VBA, set a cell’s fill color with Range("A1").Interior.Color = RGB(255, 0, 0); the three numbers are red, green, and blue…
- Excel VBA Compile Error: Common Causes and How to Fix ThemAn Excel VBA compile error means the Visual Basic for Applications editor cannot compile some code in the project, usually because of…
- Excel VBA Can’t Find Project or Library: Causes and FixesIn Excel VBA, “Can’t find project or library” usually means the VBA project has a broken or unavailable reference, not that the…