Hi ,
See if this link helps :
http://www.i-programmer.info/ebooks/automating-excel/1714-getting-at-the-data.html?start=1
In brief , I can say that every reference to an object should be fully qualified.
When we refer to a worksheet cell using the
Cells method , Excel assumes that you wish to refer to the active sheet.
Thus , if your active sheet is named
Sheet3 , then both of the following will return the same result :
Cells(6,2).Value
Worksheets("Sheet3").Cells(6,2).Value
However , what if you wished to refer to the cell D17 in the sheet named
Sheet7 , while your active sheet remained
Sheet3 ?
You would have to fully qualify the reference by using :
Worksheets("Sheet7").Cells(17,4).Value
Now what if you wished to access the values in cells D17 , D18 , D19 and D20 in the sheet named
Sheet7 ?
You would have to use fully qualified references for all of the 4 cells , as follows :
Worksheets("Sheet7").Cells(17,4).Value
Worksheets("Sheet7").Cells(18,4).Value
Worksheets("Sheet7").Cells(19,4).Value
Worksheets("Sheet7").Cells(20,4).Value
Instead of typing out these lengthy references , you can use the With ... End With statement as follows :
Code:
With Worksheets("Sheet7")
.Cells(17,4).Value
.Cells(18,4).Value
.Cells(19,4).Value
.Cells(20,4).Value
End With
Within a
With ... End With block , any reference which starts with a dot sign . , refers to an object whose fully qualified reference would include the reference used in the
With line of code. A worksheet cell reference which did not start with a dot . sign , would continue to refer to the active sheet.
Narayan