ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   peculiar error with Range() object (https://www.excelbanter.com/excel-programming/366081-peculiar-error-range-object.html)

Jeff[_42_]

peculiar error with Range() object
 
Try out the following code. If sheet2 is active, all 3 procedures work fine, but if sheet2 is not
active, TestRangeCells will not work but the other 2 procedures work fine. Anyone know why there is
a difference?

Sub TestRangeA1()
Sheets(2).Range("a1:a2") = 99
End Sub

Sub TestRangeCells()
Sheets(2).Range(Cells(1, 1), Cells(2, 1)) = 99
End Sub

Sub TestCells()
Sheets(2).Cells(1, 1) = 99
Sheets(2).Cells(2, 1) = 99
End Sub



Ardus Petus

peculiar error with Range() object
 
When used without a qualifier, Cells function will refer to ActiveSheet

You should write:

With Sheets(2)
.range(.cells(1,1),.cells(2,1)) = 99
end with

HTH
--
AP

"Jeff" a écrit dans le message de news:
...
Try out the following code. If sheet2 is active, all 3 procedures work
fine, but if sheet2 is not active, TestRangeCells will not work but the
other 2 procedures work fine. Anyone know why there is a difference?

Sub TestRangeA1()
Sheets(2).Range("a1:a2") = 99
End Sub

Sub TestRangeCells()
Sheets(2).Range(Cells(1, 1), Cells(2, 1)) = 99
End Sub

Sub TestCells()
Sheets(2).Cells(1, 1) = 99
Sheets(2).Cells(2, 1) = 99
End Sub





Jeff[_42_]

peculiar error with Range() object
 
aaaaaaahhhhh!!! Very nice! THANKS! I was thinking that cells was a property of the range object
and that you only had to qualify .range

"Ardus Petus" wrote in message
...
When used without a qualifier, Cells function will refer to ActiveSheet

You should write:

With Sheets(2)
.range(.cells(1,1),.cells(2,1)) = 99
end with

HTH





All times are GMT +1. The time now is 08:51 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com