07-27-2023, 11:26 AM
Example:
Sub FormatCellsInRange()
Dim ws As Worksheet
Dim formatRange As Range
'Set the worksheet where the data is located
Set ws = ThisWorkbook.Sheets("Sheet1")
'Set the range of cells to be formatted (adjust the range as per your data)
Set formatRange = ws.Range("A1:B5")
'Apply formatting to the range
With formatRange
.Font.Bold = True
.Interior.Color = RGB(255, 255, 0) 'Yellow color
End With
End Sub
Explanation:
The code defines a subroutine named FormatCellsInRange.
It declares variables for the worksheet (ws) and the range of cells to be formatted (formatRange).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the data is located.
Set formatRange = ws.Range("A1:B5") sets the range A1:B5 to be formatted.
Adjust the range as per your data.
The With block is used to apply formatting to the formatRange.
.Font.Bold = True makes the font bold for all cells in the range.
.Interior.Color = RGB(255, 255, 0) sets the background color of all cells in the range to yellow using the RGB value (255, 255, 0).
Sub FormatCellsInRange()
Dim ws As Worksheet
Dim formatRange As Range
'Set the worksheet where the data is located
Set ws = ThisWorkbook.Sheets("Sheet1")
'Set the range of cells to be formatted (adjust the range as per your data)
Set formatRange = ws.Range("A1:B5")
'Apply formatting to the range
With formatRange
.Font.Bold = True
.Interior.Color = RGB(255, 255, 0) 'Yellow color
End With
End Sub
Explanation:
The code defines a subroutine named FormatCellsInRange.
It declares variables for the worksheet (ws) and the range of cells to be formatted (formatRange).
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the data is located.
Set formatRange = ws.Range("A1:B5") sets the range A1:B5 to be formatted.
Adjust the range as per your data.
The With block is used to apply formatting to the formatRange.
.Font.Bold = True makes the font bold for all cells in the range.
.Interior.Color = RGB(255, 255, 0) sets the background color of all cells in the range to yellow using the RGB value (255, 255, 0).