Excel 2007 VBA tutorial-background color of controls


VBA code set background color of controls

In this example, you will learn VBA code to set background color of controls on the form. The controls on the form are stored in the controls collection of the form. The controls on the form are objects. Therefore, we need a variable declared as an object type to refer to each control in the collection. To set the background of a control, the BackColor property of the control used.

To run this example VBA code, you will need a Form and some controls such as Label, TextBox, Button, etc.

VBA for Excel: VBA code example of using VBA code to set background color of controls.
VBA code to set background color of controls:

 Private Sub CommandButton1_Click()
   Dim obj As Object 'object variable
   Dim i As Integer

   For i = 0 To Me.Controls.Count - 1
     Set obj = Me.Controls(i) 'refere to each control
     obj.BackColor = RGB(200, 10, 10) 'set background color
  Next
 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.