VBA:获取相对于特定范围的活动单元格位置?

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

VBA: getting the active cell position relative to a specific range?

vbaexcel-vbaexcel

提问by Nicholas Chan

Say I have a dim range B2:E7. How do I find out the coordinates of an active cell relative to the range?

说我有一个dim range B2:E7. 如何找出活动单元格相对于范围的坐标?

回答by CommonGuy

If you want to get the distance to the range from the active cell, this will work:

如果您想从活动单元格获得到范围的距离,这将起作用:

Sub colOffset()
  Dim testRange As Range
  Dim colLeft As Integer
  Dim colRight As Integer

  Set testRange = Range("B2:E7")
  colLeft = testRange.Column
  colRight = testRange.Column + testRange.Columns.Count - 1

  MsgBox "column-offset to first column: " & ActiveCell.Column - colLeft
  MsgBox "column-offset to last column: " & ActiveCell.Column - colRight
End Sub

Replace column with row to get the same for the rows.

用行替换列以获得相同的行。