选择,活动表,活动单元等 [英] .Select, .Activesheet, .Activecell etc...

查看:159
本文介绍了选择,活动表,活动单元等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个问题,我参考以下帖子来澄清自己:

为什么VBA添加条件格式偏移?

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

在许多情况下,许多我看到这些天的帖子,OP默认允许使用.Activate,.Select,.Offset等等,而他们是潜在的错误(通常是由最终用户造成的)的敞开大门。
$ b $有时甚至支持代码。

In many, many posts I see these days, OP's are silently allowed to use .Activate, .Select, .Offset, etc... while they are an open door to potential bugs (most often caused by the end users).
The code is sometimes even supported.

我的问题:有没有一个有效的情况,你可以使用任何这些语句,而没有直接的替代方法可以捕获这些stmts产生的典型错误? strong>

My question: Is there one valid situation where you would use any of these statements without direct alternatives being available that catch typical bugs resulting from these stmts?

我指的是我认为在开发Excel时必须的动态解决方案。
个人来说,6年以上我不记得我需要的一个案例似乎总是成为最糟糕的选择之一。在我以前的公司,这是一个沉默的规则,永远不会使用它,它只会使我的VBA生活(和终端用户)的生活更好。

I'm referring to dynamic solutions that in my opinion are a must when developing for Excel. Personally, in more than 6 years I can't remember a single case where I needed it; it seems always to be one of the the worst options available. In my previous company, it was a silent rule never to use it and it only made my VBA life (and that of the end user) better.

为什么我创建这个问题是因为我认为让新手进入VBA意识到使用这些语句时会遇到的风险是值得的(根据经验证明的风险,最终用户做了一些意想不到的事情 - 最终他们对VBA没有任何的感情),并提出直​​接的替代方案(我不会声明我一直在自己这样做,但我觉得在我的直觉中有一些错误只是为已经存在的bug怪物提供快速解决方案)。

Why I create this question is because I think that it is worthful to make newcomers into VBA aware of the risks they take when using these statements (by experience proven risks when the End Users do something unexpected - in the end they don't have any affection with VBA) and to propose direct alternatives (I won't state I always did that before myself, but I feel in my gut that there is something wrong with just offering quick solutions on already bug monsters).

我相信,当默认允许(在这种情况下它会自动增强)),启动VBA开发人员将会以错误的方式创建越来越多的工具(因此也会新手继承行为 - 他们也将从Stack Overflow中学习,因为Google返回他们寻找的结果(!))。

如果开发人员不知道为什么他可以使用select和哪种情况是潜在的错误,他不应该使用它imho。就个人而言,我可以使用立即窗口中的select stmt对动态范围定义(错误模式)进行快速检查,但不能以书面代码进行。

I believe that when silently allowed (which it automatically enhances in this case), starting VBA developers will create a growing amount of tools the wrong way (and thus also newcomers will inherit the behaviour - which they will also learn from Stack Overflow since Google returns the results they look for (!)).
If the developer is not aware why he "can" use a "select" and in which situations it is a potential bug, (s)he should never use it imho. Personally I might use the select stmt in the immediate window to do some quick checks on dynamic range definition (bug mode), but not in written code.

结果使VBA最终不再受欢迎,在出现故障的情况下,语言将成为受害者(但是仍然是Excel和Access应用程序可用的最佳编程支持)。在VBA总是屎的大型公司中,我看到过这样的事情太多了。

The result makes VBA in the end even more unpopular than it is already; the language will be made the victim in case trouble appear (yet it is imho still the "best" programming support available for the Excel and Access applications). I've seen this happen too many times in a large company where VBA is always "shit".

这只是我自己的诚实经历。

这不是对或错的问题;我有兴趣听到你对这个问题的看法。

This is only my own honest experience.
It is not a question of being right or wrong; I am interested in hearing your point of view on the question.

推荐答案

我同意选择和激活,而不是ActiveWorkbook,ActiveSheet和ActiveCell(我同意他们被滥用,但不是他们应该避免,本身)。那些肯定是合法的用途。我有一个程序可以自动执行ActiveCell中的填充系列。我的程序无法预测什么细胞将被使用;这是用户选择它。这是用户界面的一部分。

I agree about Select and Activate, but not ActiveWorkbook, ActiveSheet, and ActiveCell (I agree that they are abused, but not that they should be avoided, per se). There are definitely legitimate uses for those. I have a program that automates a "fill series" that does so from the ActiveCell. My program can't predict what cells will be used; it's up the user to select it. That's part of the user interface.

然而,有三种情况我不得不使用选择(现在四个我阅读关于缩放,但我不会使用它)。

However, there are three situations where I have had to use Select (now four that I read about zoom, but I don't ever use it).


  1. 条件格式。使用Application.ConvertFormula有一个工作,但比仅存储选择,选择正确的单元格,执行契约和重新选择上一个选择更糟糕。

  2. 数据验证。相同的原因。

  3. 形状。我希望我可以记住细节,但是我已经和形状一起工作已经很久了。没有首先选择形状,我无法做到这一点。

选择和激活的隐藏代码是一种高贵的战斗。

Ridding code of Select and Activate is a noble fight.

这篇关于选择,活动表,活动单元等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆