I am building a code to re-format (underlining, coloring)parts of any active worksheet (not a specific one). The code works but it is limited in that I am setting cells arbitrarily.
That is, for the first formatting function
below what I am using m=2 To 500 n = 2 To 200
and I am testing the macro in a worksheet which contains values in A1:L150. What I really need is to define dynamically the active area (except for first row and first column) i.e the worksheet area that has values. So for example if a worksheet has values in the range A2:M1055 I would need the formatting to happen in this area. If yet another worksheet has cells populated in the area A2:O1500 formatting would cover that latter area. So the user would not have to define the area ideally (unless there is no other way). Same for the second formatting
: I would need the entire column.
- Is there a way to achieve this using Cells() ideally?
- If not what would be an alternative?
The worksheet is not a table and has many blank cells.
Option Explicit
Dim ws As Worksheet
Dim m As Long
Dim n As Long
Sub format
Set ws = ActiveWorkbook.ActiveSheet
ws.Activate
'formatting sheet except first row and first column
For m = 2 To 500
For n = 2 To 200
If Cells(m, n).HasFormula = False Then
Cells(m, n).Interior.Color = RGB(0, 255, 0)
ElseIf Cells(m, n)...
End If
Next n
Next m
Set ws = ActiveSheet
ws.Activate
'formatting first column
For m = 2 To 100
For n = 1 To 1
If Cells(m, n).HasFormula = False Then
Cells(m, n).Interior.Color = RGB(255, 0, 0)