.Select, .Activesheet, .Activecell etc…

前端 未结 5 479
傲寒
傲寒 2021-01-13 04:40

For this question, I refer to the post below to clarify myself:
Why is my conditional format offset when added by VBA?

In many, many posts I see these days, OP\'

5条回答
  •  时光取名叫无心
    2021-01-13 05:17

    I think it is important in this matter to distinguish some:

    • Active-something: Only use this if it is absolutely necessary to know what the user is handling right now. In my experience, this is usually Data Validation or Active Sheet Detection (e.g. "Update the Sheet where the user just pressed a button").
    • Selection: Somewhat the same as Active, only use readingly. Userful either for Data Validation, or for gimmicks like "Interpret the cell value as path and open it in a new Explorer Window".
    • Select, Activate: Imho different from Selection, as it actually changes the selected Cell, Sheet etc. Never ever use this to read or write data, since it enables a user to mess up your program by just clicking. Users love to click. Only use this to Zoom (see answer by @user3357963) or clean up a view after your code has finished working (see answer by @enderland). (I'm not sure, but I think handling the PageView also requires ActiveSheet).
    • Select, Activate the 2nd: If you are new to VBA and are learning via Macro Recorder, you will find a lot of code generated like this: First Range("A5").Select, then Selection.Value="NewValue". Join this to Range("A5").Value="NewValue".
    • Offset: Personally, I don't have a problem using .Offset() - I never encountered problems with this command. Instead, I think it's a handy way of saying "The cell next to this" without having to go through "This cell's sheet at this cell's row and column+1" every time.

    In many, many posts I see these days, OP's are silently allowed to use .Activate, .Select, .Offset, etc...

    I agree with this. Even though it's easier to just give the necessary answer to make a piece of code work, the use of ActiveCell.Value and the like should be discouraged. This will be much easier if there's a well explained Thread to link to, as this here is hopefully becoming :-)

提交回复
热议问题