按位置选择单元格(左侧,顶部)

时间:2015-09-14 19:33:37

标签: excel vba excel-vba coordinates

我正在创建销售渠道地图并使用.Left / .Top w / +(。5 * .width / .Height)来获取我正在连接的图像的中心。我还想使用此方法来选择与此坐标对应的单元格。

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

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

2 个答案:

答案 0 :(得分:3)

以下是根据x和y位置选择单元格的例程:

Artist artist = new Artist();
Track track = new Track();

model.addAttribute("artist", artist);
model.addAttribute("track", track);


return "uploadForm";

答案 1 :(得分:2)

我假设您可以访问从中获得所需位置的形状对象。如果是这样,你可以做类似

的事情
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属性将远远优于迭代,即使这意味着你正在迭代该范围内的单元格或其他一些实现。