The
basic building block of an Excel Sheet is a Cell. A group of transforms
to Range, a group of range into sheet and Sheets into Workbook. If
we can access, cell and range easily in an Excel sheet, everything will
become cake walk (this is my belief). When I started to work with Excel,
in the beginning I always faced this problem. And this really made be
sad and depressed. Even for a small activity
or operation, I used to spend minutes thinking, how to address the
issue. Later, I created my own style and till date I follow it
judiciously.
I will share it here.
1. Selecting a cell A1
Worksheets(“Sheet_Name”).Range(”A1”).Select
2. Selecting a range from
A1 to A10
Worksheets(“Sheet_Name”).Range(“A1:A10”).Select
3. Now, this is important
We
know there is some data in Column A, but we are unaware of the
beginning as well as the last cell of it. How to select the range in
this case?
'Select the first Cell of Column A i.e. A1 and use function End(xlDown)
'This will take to the first entry in column A
Set Start_Row = Sheets("Sheet1").Range("A1").End(xlDown)
'Select the last cell of Column A i.e. A1000 and use function End(xlup)
'This will take to the last entry in column A
Set End_Row = Sheets("Sheet1").Range("A1000").End(xlUp)
'This selects data in column A including first and last data
Worksheets("Sheet1").Range("A" & Start_Row.Row & ":A" & End_Row.Row).Select
'copies the selection
Selection.Copy
'Selects the Cell B10 in the same sheet
Sheets("Sheet1").Range("B10").Select
'Data from Column is copied to Column B
ActiveSheet.Paste
After executing above code, we will get following output.
Hope this helps everybody. Happy VBA...ing