Learn and Create Macros Easily - Colouring TABs with VBA (Macro)

Welcome to the blog. Without wasting anytime we will get straight to the point. Here we will learn and create Macros in a very easy way.


In this blog we will colour tabs with different colours for each tab. Here  is the Data which we will be using to colour the Tabs. In this sample data, there are 10 Cities and Sheets with these city names. You are tasked to colour these 10 Tabs with colour. Each tab colour should be different from each other.  Now let  us write code which will colour tabs. 


The Code will look Like this



You can copy the code from below and use it in your excel


-----------------

Sub Change_Tab_Colour()

    


    ActiveWorkbook.Sheets("Bengaluru").Tab.Color = RGB(255, 0, 0)

    ActiveWorkbook.Sheets("Chennai").Tab.Color = RGB(0, 255, 0)

    ActiveWorkbook.Sheets("Hydrabad").Tab.Color = RGB(0, 0, 255)

    ActiveWorkbook.Sheets("Trivindram").Tab.Color = RGB(0, 0, 0)

    ActiveWorkbook.Sheets("Delhi").Tab.Color = RGB(255, 255, 255)

    ActiveWorkbook.Sheets("Kolkata").Tab.Color = RGB(238, 130, 238)

    ActiveWorkbook.Sheets("Mumbai").Tab.Color = RGB(55, 50, 255)

    ActiveWorkbook.Sheets("Pune").Tab.Color = RGB(255, 165, 0)

    ActiveWorkbook.Sheets("Mysuru").Tab.Color = RGB(255, 165, 0)

    ActiveWorkbook.Sheets("Chandigarh").Tab.Color = RGB(255, 100, 100)

    

End Sub

----------------------


Now let us learn how to write the code with explanation 


Note: You can use the previous blog to create the tabs with city names.


On your excel press Alt+F11

You will see this screen




Then click on insert-> module




You will see this . This is the place where our code will be written.




This is the module and we need a macro or code  to run this. Let us name this Change_Tab_Colour. Hence we will write 'Change_Tab_Colour' and press enter. As soon as we press end enter the 'End Sub' appears.  Our code will be between these Sub and End Sub





To toggle between excel sheet and code we need to press Alt+F11




Now let us try to understand what the code means 

The code is self explanatory. The RGB in the code stands for Red Green Blue. When it is written RGB(255,0,0) it means 255 parts of Red 0 Parts of Green and 0 parts of Blue. So it is the highest intensityRed. Similary when it written RGB (0,255,0)  it means 0 parts of Red 255 Parts of Green and 0 parts of Blue. So it is the highest intensity Blue. Each parameter (red, green, and blue) defines the intensity of the color with a value between 0 and 255. This means that there are 256 x 256 x 256 = 16777216 possible colours! Still if you find it difficult please comment below, will respond to you.

Press Alt+F8 to activate the macro. This will bring up this screen. Click on Run Button. It will run the macro and the final spread sheet will have all the TABs coloured.






How to write code with looping instead of writing line by line for each of TABs will be discussed in the next blog. Until then keep learning and bye!!




Comments

Popular posts from this blog

Learn and Create Macros Easily - Creating multiple sheets with VBA (Macro)

Learn and Create Macros easily - Hide and Unhide sheets