Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting object variable to activecell 2

Status
Not open for further replies.

SgtPepps

IS-IT--Management
May 1, 2002
109
GB
I've used the code:

Set MyRange = Worksheets("MySheet").ActiveCell

As far as i'm aware this should work, does anyone know why it doesn't?

Regards

Mike
 
Activecell doesn't have a worksheet prefix as there can only be ONE activecell at any one time

Set myRange = Activecell

will work just fine Rgds
~Geoff~
 
The ActiveCell property only works with the 'Window' object, and doesn't allow you to specify a sheet name. In fact, you can use ActiveCell without any qualifiers to return the ActiveWindow's ActiveCell. However, it can refer to the active cell on any sheet by activating the sheet before before using ActiveCell, for example:
Code:
Sheets("Accounting Errors").Activate
Set myRange = ActiveCell

Here are a couple of functions to play with:
Code:
Function ActiveSelectionRange(ByVal strSheet As String) As Range
  Sheets(strSheet).Activate
  Set ActiveSelectionRange = Selection
End Function

Function ActiveCellRange(ByVal strSheet As String) As Range
  Sheets(strSheet).Activate
  Set ActiveCellRange = ActiveCell
End Function
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Cheers Guys, All very helpfull!

Regards

SgtPepper
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top