通过它的位置select一个单元格(左,顶部)

我正在创build销售渠道地图,并使用.Left / .Top w / +(.5 * .width / .Height)来获取我要连接的图像的中心。 我也想用这个方法来select对应这个坐标的单元格。

我能想到的唯一解决scheme(可以实现,但我宁愿避免迭代方法)将会是这样的:

Sub FindCellLoc(DesiredYLocation,DesiredXLocation) 'Finds the Column Number of the X coordinate RunningTotalX = 0 For X = 1 to 100000000 RunningTotalX = RunningTotalX + Cells(1,X).width if RunningTotalX >= DesiredXLocation then TargetCol = Cells(1,X).Column Goto FoundCol End if Next X FoundCol: 'Finds the Column Number of the X coordinate RunningTotalY = 0 For Y = 1 to 100000000 RunningTotalY = RunningTotalY + Cells(Y,1).width if RunningTotalY >= DesiredYLocation then TargetRow = Cells(Y,0).Column Goto FoundRow End if Next Y FoundRow Cells(TargetRow,TargetCol).Select End Sub 

我真的很感谢任何有关非迭代方法的意见。

谢谢,-E

这是一个基于x和y位置select单元格的例程:

 Public Sub SelectCellByPos(x, y) With ActiveSheet.Shapes.AddLine(x, y, x, y) .TopLeftCell.Select .Delete End With End Sub 

我假设你有权访问你得到所需位置的形状对象。 如果是这样,你可以做类似的事情

 Function GetCenterCell(shp As Shape) As Range Dim lRow As Long, lCol As Long lRow = (shp.TopLeftCell.Row + shp.BottomRightCell.Row) \ 2 lCol = (shp.TopLeftCell.Column + shp.BottomRightCell.Column) \ 2 Set GetCenterCell = shp.Parent.Cells(lRow, lCol) End Function Sub test() Dim shp As Shape Set shp = Sheet1.Shapes(1) Debug.Print GetCenterCell(shp).Address End Sub 

如果没有一个确切的中间,那不会给你确切的中间。 它将扭曲顶部和左侧的整数除法截断(我认为)。 但是使用TopLeftCell和BottomLeftCell属性将远远优于迭代,即使这意味着您正在遍历该范围中的单元格或其他实现。