Didn't mean to sound confusing
1. I was saying that Excel and VBA have a rich set of properties in the object model, so I try to use them as much as I can
For example, the Worksheet object has a .Rows property that returns the rows on the worksheet as a Range object, and the .Rows.Count property on a Range object tells you how many rows are in the range
So instead of using a 'Magic Number' like 1000 (a magic number is a number that just shows up) in
N = Range("A1000").End(xlUp).Row + 1
to get the next blank row, I (again it's just personal style) like to use something like
N = Worksheets("Data").Cells(Worksheets("Data").Rows.Count,1) .End(xlUp).Offset(1,0).Row
So are you saying that with/end with, we can add multiple properties and methods to an object?
Not exactly. It saves having to repeatedly specify the object. The With + End With is sort of a shortcut to save typing (for me that means less chance of error) and to improve readability
The 'dot' prefix is used to 'tie' something to it's parent so just using <dot>Cells(...) the 'With object is the parent
With Worksheets("Data")
N = .Cells(.Rows.Count,1) .End(xlUp).Offset(1,0).Row
.Cells(N,1).value = "Yes"
.Cells(N,2).Resize(1,10).Interior.Color = vbRed
.Cells(1,1).CurrentRegion.EntireColumn.Autofit
End With
No Magic Numbers and will work regardless of the number of WS rows in the version of Excel being used
2. I think it's very important to explicitly refer to objects
Say there are two sheets, "Data" which has 100 rows used and "SomethingElse which has 500 rows used
Using the bit of code just above, doesn't matter if Data is the ActiveSheet or if "SomethingElse" is the Activesheet, I'll get "Yes' and 10 red cells in row 101
If I were less explicit (below) and just ASSUMED that Data was always going to be the Activesheet when the macro was run, it would go to which ever the Activesheet really was (you know you can never trust users to leave things alone)
N = Cells(.Rows.Count,1) .End(xlUp).Offset(1,0).Row
Cells(N,1).value = "Yes"
Cells(N,2).Resize(1,10).Interior.Color = vbRed
Using the bit of code just above, I could get "Yes' and 10 red cells in row 101 of "Data" or I could get "Yes' and 10 red cells in row 501 of "SomethingElse"