下拉不会选择水平选择

时间:2013-10-07 08:40:38

标签: excel excel-vba drop-down-menu vba

我在excel电子表格中创建了一个下拉列表,并在横轴上选择了值。问题出在下拉菜单中,只显示第一个值。

我可以通过在页面加载时将值输入宏来解决这个问题吗?

以下是我的选择:

Date
Incident
Problem
End to End Outage
Service Outage
Client
Service
Area
Business Area
Fact
Cause
Action
Due Date
Owner
Root Cause Code
Strategic Client Impact
Completed Date
PM Owner
Region
IFC
# of Strat Clients Impacted
Downtime Minutes
Internal Impact Only
Comments

1 个答案:

答案 0 :(得分:0)

您需要将水平单元格中的值存储在字符串中,然后将其用于数据验证。

<强>假设:

假设您的数据如下所示,并且您希望在单元格A1:M13中显示D4的列表

enter image description here

<强>代码:

Sub Sample()
    Dim ws As Worksheet
    Dim aCell As Range, rng As Range
    Dim sList As String

    '~~> Set this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> This is the range which has the horz list
        Set rng = .Range("A1:M1")

        '~~> Get the values of each cell and store it in a string
        For Each aCell In rng
            sList = sList & "," & aCell.Value
        Next

        sList = Mid(sList, 2)

        '~~> Adding the data validation to say Cell D4
        With .Range("D4").Validation
            .Delete

            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:=sList

            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
    End With
End Sub

<强>输出:

运行上面的代码时,您将看到结果

enter image description here