使用 CTRL+A 快捷键模拟选择块的 VBA 代码是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11541863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the VBA code to emulate selecting a block with the CTRL+A shortcut?
提问by oscilatingcretin
In earlier versions of Excel, pressing CTRL+A in a worksheet would literally select all cells. In Excel 2010 (not sure about 2007 or 2003), I've noticed that if you press CTRL+A within a block of cells that contain values, it seems to know to select only the cells in that block. For example, if all cells in range A1:D10 contain values and you hit CTRL+A while the active cell is in that range, it will select only A1:D10. If you press CTRL+A again, only then will it actually select all cells in the worksheet.
在早期版本的 Excel 中,在工作表中按 CTRL+A 会按字面意思选择所有单元格。在 Excel 2010(不确定 2007 或 2003)中,我注意到如果在包含值的单元格块中按 CTRL+A,它似乎知道只选择该块中的单元格。例如,如果范围 A1:D10 中的所有单元格都包含值,并且当活动单元格在该范围内时按 CTRL+A,它将仅选择 A1:D10。如果再次按 CTRL+A,它才会真正选择工作表中的所有单元格。
So I recorded a macro to see what macro code was being generated when I do this, but it actually writes Range("A1:D10").Select
when I hit CTRL+A. This is limiting and not dynamic because now I have to write my own logic to determine the boundaries around the active cell. That's not difficult with methods like ActiveCell.End(xlDown)
, but I'd like to not have to reinvent a wheel here.
因此,我录制了一个宏,以查看执行此操作时生成的宏代码,但它实际上是Range("A1:D10").Select
在我按 CTRL+A 时写入的。这是限制性的而不是动态的,因为现在我必须编写自己的逻辑来确定活动单元格周围的边界。使用 之类的方法并不难ActiveCell.End(xlDown)
,但我不想在这里重新发明轮子。
Is there some Excel VBA method like ActiveCell.GetOuterRange.Select
? That would be nice.
有没有类似的 Excel VBA 方法ActiveCell.GetOuterRange.Select
?那样就好了。
回答by Alex K.
For all dirty cells you can;
对于所有脏细胞,您都可以;
ActiveSheet.UsedRange.Select
Or for cells surrounding the current cell in a contiguous fashion you can;
或者对于以连续方式围绕当前单元格的单元格,您可以;
ActiveCell.CurrentRegion.Select