VBA查找select的angular落

给定一个矩形Selection ,我如何find每个angular落的Cell ? 具体来说,右上angular和左下angular。

例如,如果用户select范围B2:G9 ,我想要从Selection对象中获取每个angular的地址。

选择角落

我用。 .Address来得到左上angular和右下angular,而我可以开始拆分string,并执行正则expression式replace他们我想知道是否有一个更干净的方式。

你的意思是像下面的代码。

注意 :最好远离SelectSelection ,你可以尝试使用With Range("B2:G9")而不是在下面的代码中实现

 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 
  top_right_row=selection.rows(1).row top_right_col=(selection.columns(1).column+selection.columns.count-1) bottom_right_row=(selection.rows(1).row+selection.rows.count-1) bottom_right_col=selection.columns(1).column 

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