VBA example - Excel 2007: colors of columns-ColorIndex


VBA code to set colors of columns-ColorIndex

In this example, you will learn how to set the interior color of 9 columns of Excel sheet using VBA while loop. To set the interior of the columns, the ColorIndex property of of Interior object can be used.

To run this VBA example code, you can copy and paste the VBA code below in to the block of Worksheet_SelectionChange() procedure. To open this block, double-click the current worksheet, then in the objects dropdown list select Sheet1. The code work and the columns(from 1 to 9) are set to different colors when you click on any cell in the worksheet.

VBA MS Excel: VBA example code to set color of columns using ColorIndex

VBA code for this example:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)  
  
Dim i As Integer
    i = 1
    While i < 10  
      
Columns(i).Interior.ColorIndex = i

        i = i + 1
    Wend
End Sub

Comments





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.