Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:下拉列表-获取选定项目前后的项目_Excel_Vba - Fatal编程技术网

Excel VBA:下拉列表-获取选定项目前后的项目

Excel VBA:下拉列表-获取选定项目前后的项目,excel,vba,Excel,Vba,我的工作表中有一个下拉列表。当我进行选择时,是否可以在列表中的所选项目之前和之后获取项目 例如: 在上面的列表中- 如果用户选择“Kylo”,我想得到“Han”和“Leia”。 如果选择了“Luke”,我想得到“Leia”。 如果选择了“达斯”,我想得到“汉” 这可能吗 干杯, VJ这样就可以了: ' rename "Combobox1" to the name of your control below Private Sub ComboBox1_Change() Dim idx

我的工作表中有一个下拉列表。当我进行选择时,是否可以在列表中的所选项目之前和之后获取项目

例如:

在上面的列表中- 如果用户选择“Kylo”,我想得到“Han”和“Leia”。 如果选择了“Luke”,我想得到“Leia”。 如果选择了“达斯”,我想得到“汉”

这可能吗

干杯, VJ

这样就可以了:

' rename "Combobox1" to the name of your control below
Private Sub ComboBox1_Change()

    Dim idx As Long

    With ComboBox1 
        idx = .ListIndex
        If idx = 0 Then
            MsgBox "Next item: " & .List(idx + 1, 0)
        ElseIf idx = .ListCount - 1 Then
            MsgBox "Previous item: " & .List(idx - 1, 0)
        Else
            MsgBox "Previous item: " & .List(idx - 1, 0) & Chr(13) & "Next item: " & .List(idx + 1, 0)
        End If
    End With

End Sub

最后,我将范围导入到数组中,然后查找前后的项。下面是简单的代码。欢迎任何意见。谢谢你

Sub GetItemBeforeAfter ()
Dim aArray As Variant
Dim sItem As String
Dim iCounter As Integer
Dim iPosition As Integer
Dim sItemBefore As String
Dim sItemAfter As String
aArray = ActiveWorkbook.Sheets("#DataSheet").Range("A2:A51").Value
sItem = "Death Star"
    With Application
        For iCounter = LBound(aArray, 1) To UBound(aArray, 1)
            iPosition = .Match(sItem, .Index(aArray, 0, iCounter), 0)
            If IsNumeric(iPosition) Then
                Select Case iPosition
                    Case LBound(aArray, 1)
                        sItemAfter = aArray(iPosition + 1, 1)
                        MsgBox "No Before!"
                        MsgBox "After: " & sItemAfter
                    Case UBound(aArray, 1)
                        sItemBefore = aArray(iPosition - 1, 1)
                        MsgBox "Before: " & sItemBefore
                        MsgBox "No After!"
                    Case Else
                        sItemBefore = aArray(iPosition - 1, 1)
                        sItemAfter = aArray(iPosition + 1, 1)
                        MsgBox "Before: " & sItemBefore
                        MsgBox "After: " & sItemAfter
                End Select
            Exit For
            Else
                MsgBox "Item Not Found"
            End If
        Next
    End With
End Sub

这个问题没有足够的细节来给出一个好的答案,但据我所知,您拥有范围
#数据表中的值列表!A2:A51
和单元格
A1
中的选定项,类似于:

 Dim r As Range, c As Range
 Set r = [#DataSheet!A2:A51]
 Set c = r.Find([A1])
 If Not c Is Nothing Then
    If c.Row > r.Row Then MsgBox "Before: " & c(0)
    If c.Row < r.Row + r.Rows.Count Then MsgBox "After: " & c(2)
 End If
变暗r为范围,c为范围
设置r=[#数据表!A2:A51]
设置c=r.Find([A1])
如果不是,那么c什么都不是
如果c.行>r.行,则MsgBox“在:”&c(0)之前
如果c.Row
我认为这是可能的。您可以使用listbox的“Row Source”属性将listbox中的所有条目作为字符串,然后您可以使用split函数从该字符串创建一个数组,并最终对该数组执行任何操作。不过,上述可能是许多类似解决方案之一!谢谢布莱尼科斯。正如我在下面提到的,我没有使用组合框,它实际上是一个单元内数据验证(列表)。对于单元格内的下拉列表,有什么方法可以实现这一点吗?我不知道单元格内有一个带有数据验证的下拉列表。我还以为是个组合框呢。不管怎样,Slai的答案非常完美!:)谢谢Miqi180。但我没有使用组合框,它实际上是一个单元内数据验证(列表)。有什么方法可以在单元格内下拉列表中实现这一点吗?这太完美了!我只是稍微调整了一下以适应我的需要。添加了“LookAt:=xlWhole”,以便FIND查看整个单元格。非常感谢你!