Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code to Select Multiple Columns in an Excel Table

Tags:

I am new to Excel VBA. I need a modification in my code so that I would be able to proceed further.

I want to select multiple table columns in an excel table. Here is my code:

Dim ws As Worksheet
Dim tbl As ListObject

Set ws = Sheets("Sheet1")
Set tbl = ws.ListObjects(1)

Range("tbl[[Column1]:[Column5]]").Select

When I put the table name, it works. but I want to use variable which I have used in my code to select the table columns.

like image 900
Salman Khan Avatar asked Mar 16 '16 16:03

Salman Khan


1 Answers

You can use concatenation to use a variable as a table name.

Here is the code:

Dim ws As Worksheet
Dim tbl As ListObject

Set ws = Sheets("Sheet1")
Set tbl = ws.ListObjects(1)

Range(tbl & "[[Column1]:[Column5]]").Select
like image 167
Erin Strauts Avatar answered Oct 12 '22 11:10

Erin Strauts