0

我在我的 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
4

1 回答 1

0

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

假设:

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

在此处输入图像描述

代码:

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

输出:

当你运行上面的代码,你会看到结果

在此处输入图像描述

于 2013-10-07T08:58:51.470 回答