2010-11-02 77 views
2

我有以下VBA代碼(來自MS Access 2007)。該代碼創建一個新的工作簿並向單元添加下拉列表。這個小片段將一個下拉菜單添加到特定的單元格中,並向其中添加一些項目。從下拉框中返回文本而不是索引號

Dim myRng As Range 
Dim myDD As Dropdown 
Set myRng = wSheet.Cells(row, col) 
With myRng 
    Set myDD = .Parent.DropDowns.Add(Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height) 
    myDD.AddItem "msg1" 
    myDD.AddItem "msg2" 
    myDD.LinkedCell = .Parent.Cells(row, col + 2).Address(external:=True) 
End With 

這一切都很好,當我打開電子表格時,我得到一個組合框,我想要的和項目顯示。但是,當我從Excel下拉列表中選擇一個項目時,鏈接單元格顯示12(索引號)。我希望它顯示msg1msg2

這可能嗎?

回答

3

有幾個選項。

您可以在單元格中放置數據驗證下拉列表,而不是下拉對象。這將返回實際結果而不是索引。如果您還需要一個單獨鏈接的單元格,你可以把一個公式,簡單地複製DV電池

Sub MakeDv() 

    Dim wSheet As Worksheet 
    Dim myRng As Range 

    Set wSheet = ActiveSheet 

    Set myRng = wSheet.Cells(row, col) 
    myRng.Validation.Add xlValidateList, , , "msg1,msg2" 
    wSheet.Cells(row, col + 2).Formula = "=" & myRng.Address 

End Sub 

另一種選擇是不使用LinkedCell屬性和使用宏來寫的價值。將此宏分配給Dropdown

Sub ShowDDResult() 

    Dim dd As DropDown 

    Set dd = ActiveSheet.DropDowns(Application.Caller) 

    ActiveSheet.Cells(row, col + 2).Value = dd.List(dd.Value) 

End Sub 

如果您是從Access創建工作表,那麼您可能不太容易,因爲您必須添加宏。最後的選項是使用ListFillRange屬性來填充下拉菜單。列入名單的範圍和使用公式斷LinkedCell的拉日起出名單

Sub testdd() 

    Dim wSheet As Worksheet 
    Dim myRng As Range 
    Dim myDD As DropDown 
    Dim rList As Range 
    Dim aList(1 To 2, 1 To 1) As String 

    Set wSheet = ActiveSheet 
    Set rList = wSheet.Range("D1:D2") 

    Set myRng = wSheet.Cells(row, col) 
    aList(1, 1) = "msg1": aList(2, 1) = "msg2" 
    rList.Value = aList 

    With myRng 
     Set myDD = .Parent.DropDowns.Add(Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height) 
     myDD.ListFillRange = rList.Address 
     myDD.LinkedCell = wSheet.Cells(row, col + 2).Address 
     wSheet.Cells(row, col + 3).Formula = "=INDEX(" & rList.Address & "," & myDD.LinkedCell & ",1)" 
    End With 

End Sub 
+0

哇,謝謝你,我會將al ook並回復你 – Rippo 2010-11-02 19:42:38

+0

Option1爲我做到了!非常感謝您的詳細解答 – Rippo 2010-11-03 09:44:54

1

我試圖找到這樣做的更合適的方法那麼不妨ressurect這個問題的:)這裏的我如何解決這個問題。我創建了一個我想填充下拉列表的項目數組。然後使用這個數組,你可以返回與你從下拉列表中獲得的索引相關的字符串。

首先創建一個函數返回一個字符串數組:

' Returns a string array of drop down items 
function dditems() as string() 

    Dim array(2) As String 

    array(1) = "cats" 
    array(2) = "dogs" 

    dditems = array 

end function 

然後使用這個數組來填充下拉:使用此陣

' To populate your drop down 
sub populatedd() 

    dim dd As DropDown 
    dim i As Integer 
    dim itemsArray() As String 

    ' Create the dd object and item array 
    set dd = Worksheets("Sheet1").DropDowns("Drop Down 1") 
    set itemsArray = dditems() 

    ' Loop through the array to populate the drop down 
    for i = 1 to UBound(itemsArray) 

     dd.AddItem (itemsArray(i)) 

    next i 
end 

話又說回來,你可以使用下面的代碼來獲得與下拉選擇的索引相關聯的字符串:

' Get the string associated with the index 
sub showDDResult() 

    dim dd As DropDown 
    dim itemsArray() As String 

    ' Create the dd object and item array 
    set dd = Worksheets("Sheet1").DropDowns("Drop Down 1") 
    set itemsArray = dditems() 

    ' dd.ListIndex returns index, call to array returns correct string 
    MsgBox("Item selected is " & itemsArray(dd.ListIndex)) 
end