1

是否有任何方法/属性可用于获取范围选定项的字符串数组?

例如:
范围$B$2:$C$3将给出具有以下值的字符串数组:$B$2 $B$3 $C$2 $C$3

编辑 ::
我在这篇文章中找到了我的问题的答案
事实上,我需要使用以下程序:

    foreach (Range c in myRange)
    {
        string changedCell = c.get_Address(
            Type.Missing, Type.Missing, 
            XlReferenceStyle.xlA1, 
            Type.Missing, Type.Missing
        );  
        MessageBox.Show("Address:" + changedCell + " Value: " + c.Value2);
    }
4

1 回答 1

1

AFAIK,没有一个属性可以按照您所期望的格式提供给定范围的单元格地址。

这是获取所需内容的 VBA 代码

Function GetCellAddresses(ByVal srcRange As Range) As Variant
Dim cellAddresses As Variant

Dim cellCtr As Long
Dim cellCount As Long

Dim cell As Range

If Not srcRange Is Nothing Then
    cellCtr = 1
    cellCount = srcRange.Cells.Count
    ReDim cellAddresses(1 To cellCount)

    For Each cell In srcRange.Cells
        cellAddresses(cellCtr) = cell.Address
        cellCtr = cellCtr + 1
    Next
End If

GetCellAddresses = cellAddresses
End Function

要调用上述内容,GetCellAddresses(sheet1.Range("A1:B3"))

编辑:使用 LINQ,这可以成为 1 或 2 班轮。
例如var cellAddresses = srcRange.Select(c => c.Address);

或者

from cell in srcRange select cell.Address

于 2012-01-31T07:57:42.470 回答