Hi ,
A lot of unnecessary Selects have been used.
Prior to your first executable line of code , the Activesheet is not known. When you press the button to execute the code , the active sheet is the one on which the button has been placed ; when you run it from the VBE using F8 or F5 , the active sheet can be any other sheet.
It is always good practice to either activate the desired worksheet or qualify all references to the desired worksheet.
Consider the following 2 lines :
Sheets("Sheet1").Activate
Sheets(Range("AR1").Value).Select
The Range("AR1") is on which sheet ?
You might think that because Sheet1 has been activated prior to this statement that it refers to the cell AR1 on Sheet1 ; this is not so. If this code is placed in the section relating to say Sheet3 , then the above reference is to the cell AR1 in Sheet3.
If you want to reference cell AR1 in Sheet1 , the correct way would be to use :
Sheets(Sheets("Sheet1").Range("AR1").Value).Select
or , after you have activated Sheet1 , qualify the range reference with the ActiveSheet keyword , as in :
Sheets(ActiveSheet.Range("AR1").Value).Select
If the code is placed in the Sheet1 section itself , then obviously the Activate statement is not required at all , unless you have activated some other sheet prior to this statement.
Of course , if the code needs to refer to the sheet in whose section it has been placed , the better way would be to use the Me keyword , which will automatically resolve to the correct sheet irrespective of which sheet is the active sheet.
These 2 lines do not do anything other than some unnecessary work :
Range("A20").Select
Range("A21").End(xlDown).Select
The first line can be omitted without any consequence.
If you can explain exactly what AR1 contains , which sheet it refers to , and what you want to do , the same functionality may be achieved with fewer lines of code , and in a more efficient way.
Narayan