VBA & macros
Automate repeat work with macros and VBA code.
- VBA MsgBox Line Breaks: Constants, Examples, and FixesTo add a line break to a VBA MsgBox message, join the text with vbCrLf, as in MsgBox "First line" & vbCrLf…
- VBA MsgBox If: Check a User’s Choice with an If StatementIn VBA, use MsgBox inside an If statement to show a prompt and run different code according to the button the user…
- VBA MsgBox Icons: Meaning, Constants, and Code ExamplesVBA MsgBox icons are selected with a button-style constant: use vbCritical for a serious error, vbQuestion for a prompt, vbExclamation for a…
- VBA MsgBox: How to Display a VariableYes. To display a VBA variable in a message box, join it to a text string with the ampersand operator: MsgBox "The…
- VBA MsgBox Formatting: Buttons, Icons, and Message TextIn VBA, format a MsgBox by building its prompt text with string concatenation and line-break constants, then set its title, buttons, icon,…
- VBA MsgBox Default Button: Set It and Choose SafelyIn VBA, a MsgBox uses its first button as the default unless you specify otherwise. To make another button the initial choice,…
- VBA MsgBox Context: What It Means and How to Use ItIn VBA, the context argument in MsgBox is a numeric help-topic identifier: it tells the associated help system which topic to show…
- VBA MsgBox Constants: Buttons, Icons, and Return Values ExplainedVBA MsgBox constants control which buttons and icon appear in a message box, which button is selected by default, and how the…
- VBA MsgBox Cancel: How to Exit a Sub SafelyTo stop a VBA Sub when someone clicks Cancel, use a MsgBox that includes the Cancel button, check whether its return value…
- VBA MsgBox Carriage Return: Add Line BreaksTo put a carriage return and line break in a VBA MsgBox, join the message parts with vbCrLf: for example, MsgBox "First…
- VBA MsgBox Cancel: Buttons, Return Values, and CodeTo detect Cancel in VBA, show a MsgBox button set that includes Cancel, store the function’s result, and compare it with vbCancel.…
- VBA MsgBox Buttons: Options, Examples, and Return ValuesIn VBA, the MsgBox function’s buttons argument controls which buttons and icon appear, which button is selected by default, and sometimes how…
- VBA MsgBox Bold Text: Limits and Better AlternativesNo: VBA’s built-in MsgBox cannot make selected words bold or apply rich-text formatting. If you need bold text, use a VBA UserForm…
- VBA MsgBox Auto Close: Options and LimitationsA standard VBA MsgBox cannot be set to close itself after a number of seconds: its arguments include no timeout option. On…
- VBA MsgBox Arguments: Buttons, Icons, and ExamplesIn VBA, MsgBox displays a dialog and can return the button the user clicked. Its arguments are Prompt, optional Buttons, optional Title,…
- VBA Method to Sort Data in ExcelThe VBA method to sort data in Excel is the Sort object, configured with a sort key, a range, and a header…
- VBA Modify Pivot Tables in Excel: Examples and TipsYes. VBA can modify an Excel PivotTable by moving fields between rows, columns, filters, and values; setting report filters; changing some layout…
- VBA Make String Uppercase with UCase: Examples and TipsTo make a string uppercase in VBA, use the built-in UCase function: assign UCase("hello") to a variable and it returns HELLO. For…
- VBA MsgBox: How to Display Messages in ExcelIn Excel VBA, MsgBox displays a dialog box containing a message and optional buttons, and it can return the button the user…
- VBA Macro Recorder: Record, Edit, and Run MacrosThe VBA Macro Recorder in desktop Excel turns a sequence of actions into Visual Basic for Applications (VBA) code that you can…
- VBA Macro to Create a Pivot Table in ExcelTo create a PivotTable with VBA, select a worksheet containing a header row and data, then run a macro that builds a…
- VBA Len String Variable: Count Characters CorrectlyIn VBA, use Len(stringVariable) to get the number of characters in a string; the result is a Long. For example, if customerName…
- VBA Loop Through Dictionary Keys: Examples and TipsTo loop through the keys in a VBA Scripting.Dictionary, use For Each with a Variant control variable: For Each key In dict.Keys.…
- VBA Loop Through Dictionary Items: Keys, Values, and TipsTo loop through a VBA dictionary’s entries, use For Each on its keys, then retrieve each associated value with dict(key). To loop…
- VBA Insert Pivot Table in Excel: Practical Code GuideTo insert a PivotTable with VBA in Excel for Microsoft 365 on Windows, create a PivotCache from your source range, create a…
- VBA Jump to Next Loop: How to Skip an IterationVBA has no built-in Continue For or Continue Do statement. To jump to the next loop iteration, put the remaining work inside…
- VBA Left Function: How to Return Text from the StartIn VBA, use Left(string, length) to return a specified number of characters from the beginning of a string. For example, Left("AB-8421", 2)…
- VBA InputBox Password: How to Hide Typed TextInputBox in VBA has no password or masking option: it displays whatever the user types. In Excel for Microsoft 365 on Windows,…
- VBA InputBox Prompt: Syntax, Examples, and Common FixesIn VBA, use the InputBox function to display a prompt and collect text from a user: assign its result to a variable,…
- VBA InputBox Yes/No Questions: Use MsgBox CorrectlyVBA’s standard InputBox cannot display Yes and No buttons; it collects text instead. To ask a true yes-or-no question in Excel VBA,…