单击第一个下拉列表时,执行宏到第二个下拉列表

时间:2016-08-15 06:13:26

标签: excel vba excel-vba

问题

我有两个下拉列表,当我从第一个下拉列表中选择一个选项时,有没有办法自动选择第二个下拉列表?

使用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_ClickSample_Click相同,唯一的区别是Range("")值。 尽管我努力了所有的努力,但我得到了错误。

1 个答案:

答案 0 :(得分: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

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