无法创建包含Excel中的键和值的下拉列表

时间:2017-07-20 05:31:02

标签: excel vba excel-vba vlookup excel-2016

我在创建包含每列的名称和值的下拉列表时遇到了问题。

我想要实现的是使用显示一堆名称的下拉列表,但我希望这些名称具有 值,以便我可以在宏中使用它(值)。 例如,当我选择名为" Joe"它将包含值25和意志 输出为25。

我已经在Google上搜索了一些教程,但大部分内容都没有用。我已经粘贴了img。我想要从中淹没 面包师的名字和价值。但我不知道如何才能做到这一点。

一些例子或提示会很棒!我很乐意听取您的意见。Android documentation

1 个答案:

答案 0 :(得分:0)

这是宏代码:

Sub findValue()

Dim s As String
Dim value As Integer
Dim namesList As Range

Set namesList = Range("G3:G8") 'here you have to specify your range with names

s = InputBox("Enter the name: ")
'now you find the entered value (held in s variable) and get the value of a cell which
'is right to cell found (Offset function, then applied Value property)
value = namesList.Find(s).Offset(0, 1).Value

MsgBox "Found value is " & value

'save found value into the file
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile(yourPath) 'don't forget to enter full path to the
                                         'text file here!!
oFile.WriteLine value
oFile.Close
Set fso = Nothing
Set oFile = Nothing

End Sub

现在,该值存储在value变量下,您可以使用它执行所需的操作:)