获取在Excel中选定范围内的活动单元格?

我正在和VSTO一起工作。 在这种情况下,在button单击Excel工作表上将显示input框,我会得到选定的范围。 我想知道如何从input框中获取所选范围hac Excel工作表的活动单元格地址。

object objInputBox= Excel.Application.InputBox(prompt, field, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8); ExcelXP.Range selectedRange = objInputBox as Excel.Range; string rangeName = selectedRange.get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing); string activeCell =Application.Application.ActiveCell.get_Address(true, true, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing); 

我如何获取rangeName中的活动单元格地址?

提前致谢

以下是使用Application.Intersect可以完成的方法。 例子是VBA,但可以很容易地移植到C#。

 Sub TestinRange() Dim inputRange As Range Set inputRange = Worksheets(1).Range("B1:B5") Dim IsActiveCellInInputRange As Range Set IsActiveCellInInputRange = Application.Intersect(inputRange, ActiveCell) If IsActiveCellInInputRange Is Nothing Then Debug.Print "Nope, the ActiveCell is not within the Input Range" Else Debug.Print "Yep, the ActiveCell is within the Input Range. ActiveCell Address: " & ActiveCell.Address End If End Sub 

效率不高,但它应该工作。

 private bool IsActiveCellInRange(Range selectedRange) { foreach cell in selectedRange.Cells if (cell == activeCell) return true; } return false; } 

不知道我有你的问题,但看看这里的CellInNamedrange函数http://www.cpearson.com/excel/excelM.htm