使用 ActiveCell.Offset(0, 1).Value 在 VBA 中选择多个单元格

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

Multiple cell selection in VBA using ActiveCell.Offset(0, 1).Value

vbaexcel-vbaexcel

提问by cyberol

I'm starting in VBA and had to do a program that is able to retrieve value from a selected cell which will be used as reference.

我从 VBA 开始,必须执行一个程序,该程序能够从将用作参考的选定单元格中检索值。

I'm able to do it when I select one cell with the Activecellfunction and playing around with the ActiveCell.Offset(0, 1).Valueetc. However how to make the same thing while selecting multiple cells at the same time and be able to take the 1 cell value at a time and do Activecell.Offset...then identify the second cell value and retrieve the proper information and so on.

当我选择一个具有该Activecell功能的单元格并玩弄ActiveCell.Offset(0, 1).Value等时,我能够做到这一点。但是如何在同时选择多个单元格的同时制作相同的东西并且能够一次获取 1 个单元格值和不要Activecell.Offset...再找出第二个单元格的值,并获取正确的信息等等。

Using macro recorder I see that when I select multiple values it point to

使用宏记录器我看到当我选择多个值时它指向

Range("Y8,Y9,Y10,Y11").Select 'etc....

Thank you for your help and hope I've been precise enough on what I'm trying to do.

感谢您的帮助,并希望我对我正在尝试做的事情已经足够精确。

Many thanks olivier

非常感谢奥利维尔

回答by L42

I know this is kinda late and the OP probably has the solution but I think what he wants can be achieved by using Selectionlike:

我知道这有点晚了,OP 可能有解决方案,但我认为他想要的可以通过使用以下方法来实现Selection

Dim r As Range, c As Range

If TypeOf Selection Is Range Then Set r = Selection Else Exit Sub

For Each c In r
    '/* put the code you want here */
    Debug.Print c.Address
    DebUg.Print c.Offset(0,1).Value
Next

Posting as answer just in case someone stumbled on the same problem/issue/requirement.

发布作为答案以防万一有人偶然发现相同的问题/问题/要求。

回答by z??

Either use

要么使用

Range(ActiveCell, ActiveCell.Offset(0, 1))

or

或者

ActiveCell.Resize(1, 2)

Then you can use it that way

然后你可以这样使用它

For Each cell In Range(ActiveCell, ActiveCell.Offset(0, 1))
    Debug.Print cell.Value
Next cell

which is equivalent to

这相当于

Range(ActiveCell, ActiveCell.Offset(0, 1)).Select ' or ActiveCell.Resize(1, 2).Select
For i = 0 To 1
    Debug.Print ActiveCell.Offset(0, i)
Next i