How to Select Columns with VBA (3 Easy Ways)

In Microsoft Excel, VBA Macros help to solve various problems and automate repetitive tasks. Now, the selection of columns is one such task that we can automate using Excel’s VBA. In this article, we’ll explore 3 easy ways how to select columns with VBA. In addition, we’ll also learn to select multiple columns by number and fix selecting column not working issue.
The GIF below is an overview of the article which represents the selection of columns with VBA code.

Overview of selecting columns with VBA

In the following sections, we’ll discuss the dataset and learn about each method step-by-step.


Download Practice Workbook


3 Ways to Select Columns with VBA

In the first place, let’s consider the List of Company Information dataset shown in the B4:D12 cells which contains the Organization Name, Industry, and Country columns respectively. Here, we want to select the columns by applying the VBA code, henceforth, let’s glance at each method individually.

Dataset for vba columns select

Here, we have used the Microsoft Excel 365 version; you may use any other version at your convenience.


1. Selecting a Single Column with VBA Code

First of all, imagine a condition where you have to select an entire column using VBA macros. In fact, you can do it quite easily by applying simple code. Let’s follow these steps to learn.

📌 Steps:

  • First, navigate to the Developer tab >> click the Visual Basic button.

Opening Visual Basic from developer tab

Now, this opens the Visual Basic Editor in a new window.

  • Secondly, go to the Insert tab >> select Module.

Inserting Module

For ease of reference, copy the code from here and paste it into the window as shown below.

Sub select_single_col()

Range("B:B").EntireColumn.Select

End Sub

VBA code for selecting single column

Code Breakdown

Now, in the following section, we’ll explain the VBA code used to select columns with VBA.

  • In the first portion, the sub-routine is given a name, here it is select_single_col().
  • Next, use the Range.EntireColumn property and Select method to choose column B.

Code explanation

  • Third, click the Run button.

Running vba macro

Finally, the results should look like the image given below.

vba columns select single column


2. Applying VBA Code to Select Multiple Columns

For one thing, we can select multiple columns in a similar way to selecting a single column. Here, there is a slight modification to the code, so let’s dive in!

📌 Steps:

Sub select_multi_cols()
Columns("B:D").Select
End Sub

VBA code for selecting multiple columns

Code Breakdown
  • First of all, give the sub-routine a name, here it is select_multi_cols().
  • Second, use the Columns property and Select method to Select columns B through D.

Code explanation

  • At this point, hit the F5 key to execute the macro.

Executing vba code

Voila! The output should resemble the picture shown below.

vba columns select multiple columns


3. Choosing Range of Columns Using VBA Code

Alternatively, we can also select a range of columns using the VBA code. Now, follow these steps to learn!

📌 Steps:

Sub select_range_of_cols()
    Range("B6:D9").Select
End Sub

VBA code for selecting a range of cells

Code Breakdown
  • Initially, name the sub-routine, for example, select_range_of_cols().
  • Then, use the Range property and Select method to choose B6:D9 cells.

Code breakdown

  • Following this, tap the Run button.

Clicking run button

Finally, the end result appears in the screenshot given below.

vba columns select a range of cells


How to Select Multiple Columns by Number with VBA Code

Now, what if you want to select columns by numbers using the VBA code? Then you’re in luck! Because our next method answers this exact question.

📌 Steps:

Sub select_multi_cols_by_number()
Columns(2).Resize(, 3).EntireColumn.Select
End Sub

VBA code for choosing columns by number

  • In turn, press the Run button or the F5 key.

Pressing F5 key

Subsequently, your output should resemble the figure below.

vba columns select cells by number


How to Fix the Issue of Selecting Columns with VBA Not Working

Last but not least, if you’re experiencing issues selecting columns with VBA, then do not despair! Since the solution is described in the steps below.

📌 Steps:

  • In this case, Excel returns an error message when we attempt to run the code. In this situation, we have to use the worksheet name to correctly run the code.

vba columns select returning error

  • Afterward, correct the code as shown below.
Sub Fixing_select_col_error()
Worksheets("Columns Select Not Working").Columns("B:D").Select
End Sub

corrected vba code to select columns

  • From this point, click the Run button.

Press run button

Consequently, the required columns are selected as evident in the image below.

fixing vba columns select error


Things to Remember

As a note, here is something to ponder upon when running VBA codes. Usually, the Developer tab is hidden by default, hence to run the VBA script we have to enable the Developer tab.

📌 Steps:

  • At the start, navigate to the File tab.

Selecting File tab

  • Immediately, choose Options from the list.

go to Excel options

  • Later, in the Excel Options window, press Customize Ribbon >> tick the Developer tab >> tap OK.

Tick mark developer tab in the Customize ribbon option

Eventually, the Developer tab appears as shown in the picture below.

Developer tab enabled


Practice Section

We have provided a Practice section on the right side of each sheet so you can practice yourself. Please make sure to do it by yourself.

Practice Section


Conclusion

In essence, this article shows 3 effective methods on how to select columns with VBA. So, read the full article carefully, we hope you find this article helpful. Now, if you have any further queries or recommendations, please feel free to comment here.

Get FREE Advanced Excel Exercises with Solutions!
Asikul Himel
Asikul Himel

Asikul Islam Himel, holding a BSc in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology, has contributed over two years to the ExcelDemy project. Starting as an Excel & VBA Content Developer, now he manages projects at You Have Got This Math Project. He wrote 60+ articles for ExcelDemy, reviewed 500+, and focused on quality maintenance. Currently, his responsibilities include project management and team leadership. Himel's interests encompass data analysis, leadership, WordPress applications, and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo