I have lots of code that refers to the activesheet. In fact, I have lots of
code that refers to the selection, too. But I expect that the user wants to run
the code against the activesheet--or even just against the current selection
(say changing case in a just a few cells).
I don't usually have my code select anything--but I do trust the user (usually
me!) to select what should be acted upon first. It's not really magic--it's
more trust (must more dangerous than magic!).
And I usually use Activesheet without the dot in front of it. I want the
activesheet in the activewindow.
When you write .activesheet, that dot means something important. And the
activesheet will refer to the object in the previous "with" statement.
Dim wb As Workbook
For Each wb In Application.Workbooks
MsgBox wb.ActiveSheet.Range("a1").Address(external:=True)
Next wb
and .activesheet can belong to the application, a workbook or a window.
Since you didn't share what you used in the previous With statement, I wasn't
sure if you used it the way you wanted.
And with worksheets, .activate is very similar to .select. But if you have 17
worksheets selected and activate one of those 17, then that one will become the
activesheet--but the others will continue to be selected.
"(PeteCresswell)" wrote:
>
> Per Dave Peterson:
> >And not changing selections usually means that the code runs faster, but more
> >importantly, it really makes the code easier to read later on.
>
> I didn't want to say anything because I'm one of those people whose car
> definitely runs better after it's been washed and waxed... but it seems to me
> like I got a noticeable increase in speed after purging all the .Selects.
> >
> >And for the most part, I think it's true that your code shouldn't have any
> >.selects and .activates in them. There are a few exceptions--Freezing windows
> >comes to mind. You have to be on the sheet and be in your cell to do that.
> >
> >You wrote .activesheet. I bet you meant .activate. But if you didn't you may
> >find Activesheet in your code--especially if that code can be used lots of times
> >(deleting rows based on values in a column, for instance).
>
> No, I meant .ActiveSheet. Haven't used .Activate yet - don't even know what it
> does, although my first guess would be that it's functionally similar to
> .Select...
>
> To me, the implication of .ActiveSheet is that somehow the right worksheet has
> magically been selected. So I replaced all those references with explicit
> "theSS.Worksheets("whatever") refs - passing the worksheet name as a parm to the
> routine in question.
>
> >You may want to refer to ranges/objects by using:
> >With Activesheet
>
> Seems like a violation of the spirit of avoiding .Select - in that what's being
> referred to in the code isn't immediately obvious. Or am I misunderstanding
> .ActiveSheet?
> --
> PeteCresswell
--
Dave Peterson
|