Why set the range to "A10:L27"?
It was just an example. It can be anything you desire. I just wanted to make sure that the last row logic would work, even when it is not starting on row 1.
What do these first 3 lines mean, I have seen them around but have no idea what their purpose is.
They are variable declarations. It is good practice to declare all your variables in VBA before using them.
As matter as fact, if you type the phrase "Option Explicit" at the very top of your VBA Editor window (before all your VBA code), it will REQUIRE you to declare all your variables. This is helpful in error debugging and helps quickly identify typos in your variable names.
And last what would be the purpose of the message boxes?
It is just returning the addresses/values to the screen for you to see what they are. They are useful in testing to make sure that your code is doing what you want. But you can remove them from your final code.
Maybe I wasnt clear enough but I want those rows selected so I can apply formatting to them
That is easy enough. With Range variables, just use .Select, i.e.
For the LastRow variable, which is an Integer, you can just do this:
However, note that it is not necessary to physically select the ranges in order to work with them (as matter as fact, Selecting/Activating ranges will slow your code down).
For example, if you wanted to format MyRange to be dates, you could do it like this:
Code:
MyRange.NumberFormat = "mm/dd/yyyy"
Likewise, if you want to format the last row to be percentages, you could do it like this:
Code:
Rows(LastRow).NumberFormat = "0.00%"