VBA example - Excel 2007: tables in Excel sheet

VBA code to create tables in Excel sheet

When you work in Excel sheets that need to present data in many tables, applying cells border in a worksheet is frequently done. In this VBA code example, you will learn how to generate tables in an Excel sheet as fast as possible by selecting the cells that you want to generate a table then press Tab key.

The VBA code in this example is divided into two parts. The first part needs to be writtten in the block of Worksheet_SelectionChange() procedure of your worksheets. By doing this, the code will be executed when you make selections on cells. The second part of the VBA code will be placed in the standard module(you need to create a module).

VBA code for this example:

'First part of the VBA code in this example
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

      'Pressing Tab key to create tables

      Application.OnKey "{TAB}", "Setborder"

End Sub


'Second part of the VBA code in this example to be placed in module
Public Sub Setborder()

    With Selection

     .Borders.LineStyle = xlDouble

     .Borders.Color = RGB(10, 200, 200)

    End With

End Sub


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.