To insert two columns using Excel VBA, use the Columns("A:B").Insert method or specify a range such as Range("B:C").EntireColumn.Insert. This action shifts existing data to the right, creating two blank columns at the specified location. This is particularly useful for automating report formatting where you need to inject space for new data categories without manually right-clicking and inserting columns every time a new dataset is imported.
What You'll Need
- Microsoft Excel installed on a Windows or Mac computer.
- The Developer tab enabled in the Excel Ribbon.
- A basic understanding of how to open the Visual Basic Editor (VBE).
- An existing worksheet with data to ensure you can see the columns shifting correctly.
Step-by-Step
- Enable the Developer Tab: If you do not see the Developer tab at the top of your Excel window, right-click any existing tab on the Ribbon, select Customize the Ribbon, and check the box for Developer in the right-hand column. Click OK.
- Open the Visual Basic Editor: Click on the Developer tab and select the Visual Basic button on the far left, or press
Alt + F11on your keyboard. This opens the VBE window where your code will reside. - Insert a New Module: In the VBE, go to the top menu and select Insert > Module. This creates a blank space where you can write your macro. Writing code in a module ensures the macro is available to the entire workbook.
- Write the Subroutine: Type the following line to start your macro:
Sub InsertTwoColumns(). This names the procedure so you can identify it later in the Macro list. - Define the Target Columns: To insert two columns, you must tell VBA which columns should be pushed to the right. For example, if you want two new columns to appear before Column B and Column C, you target those specific columns. Enter the following line of code:
Columns("B:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove. - Understand the Parameters:
"B:C": This tells Excel to select two columns. Because you selected two, Excel will insert exactly two blank columns.Shift:=xlToRight: This ensures the existing data moves to the right rather than being overwritten.CopyOrigin:=xlFormatFromLeftOrAbove: This tells Excel to apply the formatting of the column to the left to the newly created columns.
- Close the Subroutine: Type
End Subon a new line to signal the end of the code block. - Run the Macro: You can run the code directly from the VBE by pressing
F5or clicking the green Play button. Alternatively, go back to your Excel sheet, click the Macros button on the Developer tab, selectInsertTwoColumns, and click Run.
Common Mistakes
One of the most frequent errors is targeting a single column while expecting two to appear. If you use Columns("B").Insert, VBA will only create one column. To get two, you must explicitly define a range of two columns (e.g., "B:C").
Another common mistake is failing to specify the worksheet. If you simply write Columns("B:C").Insert, VBA applies the command to the ActiveSheet. If the user happens to be looking at "Sheet2" when the macro runs, the columns will be inserted there instead of "Sheet1". To prevent this, always qualify your range: Worksheets("Sheet1").Columns("B:C").Insert.
Users also frequently forget to save their file as an Excel Macro-Enabled Workbook (.xlsm). If you save the file as a standard .xlsx workbook, all your VBA code will be permanently deleted the moment you close the file, requiring you to rewrite the entire script from scratch.
Lastly, avoid using Select or Activate. Beginners often record a macro and see Columns("B:C").Select followed by Selection.Insert. This slows down the code and causes the screen to flicker. Direct action (as shown in the step-by-step) is significantly more efficient.
Tips
To make your macro more flexible, use variables instead of hard-coded column letters. For example, you can use Columns(2).Resize(, 2).Insert. The number 2 represents Column B, and Resize(, 2) expands the selection to two columns. This is helpful if you need to insert columns based on a calculated position.
If you are inserting columns into a table (ListObject), the behavior is slightly different. You should target the table's range specifically to ensure the table expands to accommodate the new columns without breaking the table's structural integrity.
To optimize performance, especially if you are inserting columns as part of a larger loop, wrap your code in Application.ScreenUpdating = False at the start and Application.ScreenUpdating = True at the end. This prevents Excel from refreshing the visual display after every single action, which can make the macro run up to ten times faster.
Consider adding a check to see if the columns have already been inserted. You can do this by checking the header text of the column. If the header already says "New Data", the macro can skip the insertion step to avoid creating endless blank columns every time the button is clicked.
Troubleshooting
If you receive a "Run-time error '1004': Application-defined or object-defined error," check if your worksheet is protected. VBA cannot insert columns into a protected sheet. You must first unprotect the sheet using ActiveSheet.Unprotect "YourPassword", perform the insertion, and then protect it again.
If the columns are inserted but the formatting looks strange, check the CopyOrigin parameter. If you want the new columns to inherit the formatting from the right instead of the left, change the parameter to xlFormatFromRightOrBelow.
If the macro runs but nothing happens, ensure you are looking at the correct worksheet. If you used Worksheets("Data").Columns("B:C").Insert, but you are currently viewing "Sheet1", the changes are happening in the background on the "Data" sheet. Switch tabs to verify the results.
If the VBE crashes or freezes, check for infinite loops. If you have put the Insert command inside a Do While or For Each loop that evaluates the column count, you might be creating a loop that keeps adding columns forever because the condition for stopping is never met (since the number of columns keeps increasing). To stop a runaway macro, press Ctrl + Break or Esc repeatedly.
Frequently Asked Questions
Can I insert two columns at a specific row instead of the whole column?
No, columns are vertical structures that span the entire height of the worksheet. You can insert cells in a specific range and shift them right, but to insert "columns," the action must apply to the entire vertical axis of the sheet.
How do I insert two columns at the very beginning of the sheet?
Target columns A and B. Use the code Columns("A:B").Insert. This will push the original Column A to Column C and create two new blank columns at the start.
Will inserting columns break my existing formulas?
Generally, no. Excel is designed to update cell references automatically. If a formula was =SUM(A1:A10) and you insert two columns to the left, the formula will automatically update to =SUM(C1:C10).
Can I insert two columns based on a search term?
Yes. You can use the Range.Find method to locate a specific header name, identify which column that is, and then use the .Offset or .Resize method to insert two columns immediately to the left or right of that found cell.
Is there a difference between using Range and Columns?
Not in result, but in syntax. Columns("B:C").Insert is shorter. Range("B:C").EntireColumn.Insert is more explicit and is often preferred when you are already working with a specific cell range and want to expand the action to the full column.