2013-10-07 195 views
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 

回答

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

+0

我加入了代碼suggestd但它不會在下拉顯示? –

+0

你做了必要的修改嗎?像將表格名稱更改爲相關表格名稱,或將範圍更改爲您的範圍? –

+0

我做了....我改變了範圍設置rng = .Range(「A2:Y2」)&工作表設置ws = ThisWorkbook.Sheets(「FCA」) –