vba set color font format of cell in Excel
Q155. In Excel, how can I set the fill color, font color and set number format of cell to date?
To do it in Excel, here is the answer:
Being able to change the format of a cell with VBA is very useful.
It allows you for example to make the format of a cell or the format of a sheet flexible.
You can adapt the format with the use of a profile different for each user.
- Option Explicit
- Sub FormatCell()
- ActiveCell.Font.Color = RGB(255, 0, 0)
- ActiveCell.Interior.Color = RGB(255, 255, 0)
- ActiveCell.NumberFormat = "mm/dd/yyyy"
- End Sub
a) Line 3 sets the font color of the cell using RGB code - first argument corresponds to Red, second argument corresponds to Green and 3rd argument corresponds to Blue. In this sample case, cell is set to Red font.
b) Line 4 sets the fill color of the cell using RGB code. In this sample case, cell is set to Yellow fill since Red value of 255 and Green Value of 255 with 0 for Blue correspond to Yellow.
c) Line 5 sets the data in cell to "mm/dd/yyyy" date format.