ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   worksheet range qualifier (https://www.excelbanter.com/excel-programming/402757-worksheet-range-qualifier.html)

[email protected]

worksheet range qualifier
 
Are the last 2 statements equivalent?

Dim oReportSheet As Worksheet
oReportSheet = Worksheets.Add
oReportSheet.Range(Cells(1, 1), Cells(1,
7)).Columns.EntireColumn.AutoFit
Range(oReportSheet.Cells(1, 1), oReportSheet.Cells(1,
7)).Columns.EntireColumn.AutoFit

What if the oReportSheet sheet weren't active. Would that matter?

[email protected]

worksheet range qualifier
 
On Dec 14, 2:11 pm, "
wrote:
Are the last 2 statements equivalent?

Dim oReportSheet As Worksheet
oReportSheet = Worksheets.Add
oReportSheet.Range(Cells(1, 1), Cells(1,
7)).Columns.EntireColumn.AutoFit
Range(oReportSheet.Cells(1, 1), oReportSheet.Cells(1,
7)).Columns.EntireColumn.AutoFit

What if the oReportSheet sheet weren't active. Would that matter?


They are if oReportSheet is active. So are

Range(Cells(1, 1), Cells(1, 7)).Columns.EntireColumn.AutoFit

and

Activesheet.Range(Cells(1, 1), Cells(1,
7)).Columns.EntireColumn.AutoFit

If oReportSheet is not active, or may not be, the neatest syntax to
use is

With oReportSheet
.Range(.Cells(1, 1), .Cells(1, 7)).Columns.EntireColumn.AutoFit
end With

note the dots before Range and Cells. you must fully qualify the range
reference with the sheet object.

regards
Paul



[email protected]

worksheet range qualifier
 
On Dec 14, 9:24 am, wrote:
On Dec 14, 2:11 pm, "
wrote:

Are the last 2 statements equivalent?


Dim oReportSheet As Worksheet
oReportSheet = Worksheets.Add
oReportSheet.Range(Cells(1, 1), Cells(1,
7)).Columns.EntireColumn.AutoFit
Range(oReportSheet.Cells(1, 1), oReportSheet.Cells(1,
7)).Columns.EntireColumn.AutoFit


What if the oReportSheet sheet weren't active. Would that matter?


They are if oReportSheet is active. So are

Range(Cells(1, 1), Cells(1, 7)).Columns.EntireColumn.AutoFit

and

Activesheet.Range(Cells(1, 1), Cells(1,
7)).Columns.EntireColumn.AutoFit

If oReportSheet is not active, or may not be, the neatest syntax to
use is

With oReportSheet
.Range(.Cells(1, 1), .Cells(1, 7)).Columns.EntireColumn.AutoFit
end With

note the dots before Range and Cells. you must fully qualify the range
reference with the sheet object.

regards
Paul


thanks, Paul


All times are GMT +1. The time now is 10:57 AM.

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