VBA寻找选择的角落

时间:2017-02-14 07:31:33

标签: excel vba

如果是矩形Selection,如何找到每个角落的Cell?具体来说,右上角和左下角。

E.g。如果用户选择范围B2:G9,我想从Selection对象获取每个角落的地址。

Selection corners

我使用.Address获取左上角和右下角,虽然我可以开始拆分字符串并对它们执行正则表达式替换,但我想知道是否有更清晰的方法。

2 个答案:

答案 0 :(得分:9)

你的意思是下面的代码。

注意:最好远离SelectSelection,您可以尝试使用With Range("B2:G9")代替(在下面的代码中尚未实现)< / p>

Option Explicit

Sub GetRangeSelectionCorners()

Dim TopLeft As String, TopRight As String, BottomLeft As String, BottomRight As String
Dim TopLeftRow As Long, TopLeftCol As Long, BottomRightRow As Long, BottomRightCol As Long

Range("B2:G9").Select
With Selection
    TopLeft = .Cells(1, 1).Address '<-- top left cell in Selection
    TopRight = .Cells(1, .Columns.Count).Address '<-- top right cell in Selection
    BottomLeft = .Cells(.Rows.Count, 0.1).Address '<-- bottom left cell in selection
    BottomRight = .Cells(.Rows.Count, .Columns.Count).Address '<-- last cell in selection (bottom right)

    ' get row and column number
    TopLeftRow = .Cells(1, 1).Row '<-- top left cell's row
    TopLeftCol = .Cells(1, 1).Column '<-- top left cell's column
    BottomRightRow = .Cells(.Rows.Count, .Columns.Count).Row '<-- bottom right cell's row
    BottomRightCol = .Cells(.Rows.Count, .Columns.Count).Column  '<-- bottom right cell's column
End With

MsgBox "Top Left cell address is :" & TopLeft & vbCr & _
        "Top Right cell address is :" & TopRight & vbCr & _
        "Bottom Left cell address is :" & BottomLeft & vbCr & _
        "Bottom Right cell address is :" & BottomRight

MsgBox "Top Left cell's row is : " & TopLeftRow & _
        ", and column is :" & TopLeftCol & vbCr & _
        "Bottom Right cell's row is : " & BottomRightRow & _
        ", Bottom Right cell's column is :" & BottomRightCol

End Sub

答案 1 :(得分:1)

SELECT * FROM(
SELECT
@inactive_percentage:=((UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(APP_UPDATE_DATE))) / (UNIX_TIMESTAMP(DEL_TASK_DUE_DATE) - UNIX_TIMESTAMP(APP_UPDATE_DATE)) * 100 AS inactive_percentage,
IF(@inactive_percentage <= 33, "low", IF(@inactive_percentage >= 33 && @inactive_percentage <= 66, "normal", "high")) AS inactive

FROM APP_CACHE_VIEW
) as foo WHERE `inactive`='low'

你可以得到像这样的行和列值。如果您有任何错误,我还没有测试代码,请退回