2016-08-15 249 views
2

问题

我有两个下拉列表,是有办法来自动选择第二个下拉列表中,当我选择从第一个下拉列表中的选项?执行宏第二下拉列表中,当点击第一个下拉列表

下拉列表使用2数据透视表进行数据验证。

代码
Private Sub Worksheet_Change(ByVal Target As Range) 
Dim cell As Range 

For Each cell In Target 
    If Not Intersect(cell, Range("F9")) Is Nothing Then 
     Call Sample_Click 
    ElseIf Not Intersect(cell, Range("F10")) Is Nothing Then 
     Call Sample2_Click 
End If 
Next cell 
End Sub 

Sample_Click
Dim ACount As Integer 
Dim Dept As String 
Dim Func As String 
Dim Pos As String 

Range("F9").Select 
Dept = Trim(ActiveCell.Value) 

Sheets("Sheet1").Select 
ActiveSheet.PivotTables("PivotTable1").PivotFields("Dept").ClearAllFilters 
ActiveSheet.PivotTables("PivotTable1").PivotFields("Dept").CurrentPage = Dept 

ACount = ActiveSheet.PivotTables("PivotTable1").RowRange.Cells.Count 
ACount = ACount + 2 

Sheets("Home").Select 
    Range("F10").Select 
    With Selection.Validation 
    .Delete 
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
    xlBetween, Formula1:="=Sheet1!$A$4:$A$" & ACount 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
    End With 
    ActiveCell.Formula = "" 

问题

Sample2_Click相同Sample_Click,唯一的区别是Range("")值。 尽管我尝试了所有的努力,但我仍然收到错误信息。

+0

你得到的错误是什么,以及哪行代码给出错误? – DragonSamu

回答

0

使用相交时......还板材应该是正确的......这样你就不需要循环中的所有细胞,你需要的是:

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Not Intersect(Target, [F9]) Is Nothing Then 
    [F10].Select 
    ElseIf Not Intersect(Target, [F10]) Is Nothing Then 
    [F11].Select 
    End If 
End Sub 

...为我工作没有错误...

+0

你有没有试过用'Application.EnableEvents = False'开始你的'Sample_Click'? –

相关问题