好的。我有你正在尋找的東西。幾個月前我在另一個項目中解決了這個問題。基本上,間接是不好的,因爲它不適用於動態命名範圍,因爲它們不會產生實際結果,只是公式引用。
首先,在表單上設置您的命名範圍。命名範圍以我描述的方式命名是非常重要的,因爲這會將代碼饋入到製作動態列表中。另外,請注意,我只寫出X1和T2的SamplePoints。如果您選擇了其他選項,直到你在添加這些命名區域的代碼將無法正常工作
然後假設輸入板的設置類似於如下:
這個地點工作表中的代碼更改輸入工作表的事件。它所做的是取一個單元格中選定的值,然後附加適當的列名稱以提供該列表。因此,如果項目A被選中並且您想爲項目A選擇一個責任方,則它將範圍內的驗證(「B(無論您在哪一行)」設置爲A_Responsible,從而爲您提供該列表。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wks As Worksheet
Dim strName As String, strFormula
Dim rng As Range
Set wks = ActiveSheet
With wks
If Target.Row = 1 Then Exit Sub
Select Case Target.Column
Case Is = .Rows(1).Find("Project", lookat:=xlWhole).Column
Set rng = Target.Offset(, 1)
strName = Target.Value
strFormula = "=" & Replace(strName, " ", "_") & "_Responsible"
AddValidation rng, 1, strFormula
'add any more cells that would need validation based on project selection here.
Case Is = .Rows(1).Find("Responsible", lookat:=xlWhole).Column
Set rng = Target.Offset(, 1)
strName = Target.Value
strFormula = "=" & Replace(strName, " ", "_") & "_SamplePoint"
AddValidation rng, 1, strFormula
'add any more cells that would need validation based on responsible selection here.
'Case Is = add any more dependenices here ... and continue with cases for each one
End Select
End With
。
您還需要這個功能的標準模塊中某個工作簿中的
Function AddValidation(ByVal rng As Range, ByVal iOperator As Integer, _
ByVal sFormula1 As String, Optional iXlDVType As Integer = 3, _
Optional iAlertStyle As Integer = 1, Optional sFormula2 As String, _
Optional bIgnoreBlank As Boolean = True, Optional bInCellDropDown As Boolean = True, _
Optional sInputTitle As String, Optional sErrorTitle As String, _
Optional sInputMessage As String, Optional sErrorMessage As String, _
Optional bShowInput As Boolean = True, Optional bShowError As Boolean = True)
'==============================================
'Enumaration for ease of use
'XlDVType
'Name Value Description
'xlValidateCustom 7 Data is validated using an arbitrary formula.
'xlValidateDate 4 Date values.
'xlValidateDecimal 2 Numeric values.
'xlValidateInputOnly 0 Validate only when user changes the value.
'xlValidateList 3 Value must be present in a specified list.
'xlValidateTextLength 6 Length of text.
'xlValidateTime 5 Time values.
'xlValidateWholeNumber 1 Whole numeric values.
'AlertStyle
'xlValidAlertInformation 3 Information icon.
'xlValidAlertStop 1 Stop icon.
'xlValidAlertWarning 2 Warning icon.
'Operator
'xlBetween 1 Between. Can be used only if two formulas are provided.
'xlEqual 3 Equal.
'xlGreater 5 Greater than.
'xlGreaterEqual 7 Greater than or equal to.
'xlLess 6 Less than.
'xlLessEqual 8 Less than or equal to.
'xlNotBetween 2 Not between. Can be used only if two formulas are provided.
'xlNotEqual 4 Not equal.
'==============================================
With rng.Validation
.Delete ' delete any existing validation before adding new one
.Add Type:=iXlDVType, AlertStyle:=iAlertStyle, Operator:=iOperator, Formula1:=sFormula1, Formula2:=sFormula2
.IgnoreBlank = bIgnoreBlank
.InCellDropdown = bInCellDropDown
.InputTitle = sInputTitle
.ErrorTitle = sErrorTitle
.InputMessage = sInputMessage
.ErrorMessage = sErrorMessage
.ShowInput = bShowInput
.ShowError = bShowError
End With
End Function
這應該讓你開始:http://siddharthrout.wordpress.com/2011/07/29/excel-data- validationcreate-dynamic-dependent-lists-vba/ –
urgh ...試圖回答,但由於某種原因我無法上傳圖片。明天再回來試試! –
@SiddharthRout I've找到你的代碼,我開始着手它。這真的很好,很好解釋。另外,這就是我想要的!但問題是,當有不同的工作表時,它不起作用。主要如果...失敗,因爲交叉點來自不同的工作表。我不知道如何解決它。 – meikse