简单的 VBA 选择:选择活动单元格右侧的 5 个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16843526/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:43:32  来源:igfitidea点击:

Simple VBA selection: Selecting 5 cells to the right of the active cell

excelvbaexcel-vbaexcel-2010

提问by user2437803

I am trying to select and a row of 5 cells to the right (the selection should include my active cell) of my active cell. My current code is:

我正在尝试选择活动单元格右侧的一行 5 个单元格(选择应包括我的活动单元格)。我目前的代码是:

Sub SelectandCopy()
'
' SelectandCopy Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
ActiveCell.CurrentRegion.Select
Selection.Copy
Windows("Study.xlsx").Activate
End Sub

I do want not a specific range from my worksheet because I want to be able to make a selection of these data sets from anywhere within my worksheet. Any help would be greatly appreciated!!

我不希望工作表中有特定范围,因为我希望能够从工作表中的任何位置选择这些数据集。任何帮助将不胜感激!!

Would you use the Selection.Extend?

你会使用 Selection.Extend 吗?

回答by Jon Crowell

This copies the 5 cells to the right of the activecell. If you have a range selected, the active cell is the top left cell in the range.

这将复制活动单元右侧的 5 个单元。如果您选择了一个范围,则活动单元格是该范围左上角的单元格。

Sub Copy5CellsToRight()
    ActiveCell.Offset(, 1).Resize(1, 5).Copy
End Sub

If you want to include the activecell in the range that gets copied, you don't need the offset:

如果要在复制的范围内包含活动单元格,则不需要偏移量:

Sub ExtendAndCopy5CellsToRight()
    ActiveCell.Resize(1, 6).Copy
End Sub

Note that you don't need to select before copying.

注意复制前不需要选择。

回答by enderland

This example selects a new Rangeof Cellsdefined by the current cell to a cell 5 to the right.

本实施例中选择一个新RangeCells由当前小区定义为小区5至右侧。

Note that .Offsettakes arguments of Offset(row, columns)and can be quite useful.

请注意,.Offset接受Offset(row, columns)和 的参数可能非常有用。



Sub testForStackOverflow()
    Range(ActiveCell, ActiveCell.Offset(0, 5)).Copy
End Sub