2010-11-22 109 views
0

我想找出用戶在Excel表格中選擇的單元格的名稱。爲清晰起見,請查看下面的圖片。在示例中,我需要使用VBA宏檢索所有單元名稱(A1,B1,C1,D1,E1,F1)。如何找到使用VBA的範圍選擇上的單元格名稱

alt text

我能找出多少小區中選擇,但不知道如何找回自己的名字。

回答

3

如果您希望將名稱存儲到數組中,則需要遍歷範圍內的所有單元格,並將每個單元格的地址存儲到數組或集合中。這裏是一個示例,讓你開始:

Public Sub TestIt() 

    Dim addressArr() As String 
    Dim i As Long 

    addressArr = GetSelectedCells(Selection) 

    For i = LBound(addressArr) To UBound(addressArr) 
     MsgBox addressArr(i) 
    Next i 
End Sub 

Public Function GetSelectedCells(selectedRng As Range) As String() 

    Dim cellArr() As String 
    Dim cell As Range 
    Dim i As Long 

    ReDim cellArr(0 To (selectedRng.Cells.Count - 1)) 
    i = 0 'setup index for storing to array 

    For Each cell In selectedRng 

     cellArr(i) = cell.Address(False, False) 'modify the address here to get reference style 

     i = i + 1 
    Next cell 

    GetSelectedCells = cellArr 
End Function 
+0

謝謝Fink,cell.Address是我在找什麼,.. :) – RameshVel 2010-11-23 05:47:25

相關問題