6 - Recap of Macro recording

Hope you enjoyed the previous section. Just to recap
1. We learned basics of a workbook, worksheets range, and cells.
2. We learned how to record a macro
3. How to modify the recorded code
4. Most importantly, what and how to keep things in mind before you start with coding.

I also wanted to show you something before you proceed to next section.

I asked you to try writing code for a different color combination. However, I did not tell you what value does VBA accept for Yellow and for Pink. I have already indirectly told you how to do know that. But let me tell you what all methods I use to find that information.

1. Record Macro:
 You definitely have to use this feature now or sometime later and especially when you are dealing with formatting stuff. If you need to make multiple changes in format such as
Change the fill color of a cell or
Change the text color of the value in a cell or
Add different type of borders( normal border, thick border, outer border)
Change the format type of cells to Text instead of general.

Coding for all that can be cumbersome and there is a chance that you may not achieve what you wanted to achieve.

So, the quickest way to achieve that is record macro.
Let me show you how.

So this is our chess board. Now I will,
Change black to pink for any one of the black cells with recorded macro

Below is the code generated



Sub Macro5()
'
' Macro5 Macro
'

'
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 6684927
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub

Check the highlighted portion and compare it with the code you had for Black. You see the difference here.

Now it is your task to do the remaining changes in your code to get a Yellow Pink color combination

2. Google
The best thing about VBA is that it's very commonplace language. It has a wide range of users ranging from software developer to Test Engineer to Project Manager. Hence, if you get stuck somewhere, there is a pretty good chance that someone else would have also been through that and have sought help in public forum.
However, there are some tips I would like to share with you.
- Use keywords. If you had to search for how to change the color of a cell. You can either search for complete text or the best is to search with "Cell Color change VBA"

- There are few well known sites for such question answers
  https://www.mrexcel.com
  www.ozgrid.com 
  https://www.experts-exchange.com
  https://stackoverflow.com

No comments:

Post a Comment