VBA选择下拉列表中的每个选项

时间:2012-11-15 21:08:35

标签: excel-vba vba excel

更新代码&题: 此表的模块中的当前代码如下:

Private Sub Worksheet_Change(ByVal Target As Range)
     If Not Intersect(Target, Range("B2:B2")) Is Nothing Then
          Application.Run "MonthlyRead"
     End If
End Sub

Sub MPrintAll()
     Dim c As String
     Dim MonthlyList As Range
     Set MonthlyList = Worksheets("Monthly").Range("MonthlyList").Cells
     For Each cell In MonthlyList
         Range("b2").Value = cell.Value
         ActiveWorkbook.Worksheets("Monthly").PrintOut
     Next cell
End Sub

我一直在逐步完成代码,试图找出问题发生的位置。当我在“Range(”b2“)之后按F8。值= cell.Value”它立即转到工作表的第一行代码,完全跳过Print命令。此外,它删除命名范围中的第一个值,而不是将其复制粘贴到单元格B2。

作为参考,这是上面第一个例程调用的Module1中的代码:

    Sub MonthlyRead()
         Call MEFTPS
         Call MUCT6
    End Sub
    Sub MEFTPS()
         If Range("a2").Value = "EFTPS Package" Then
              Call MShow
              Else: Call MHide
         End If
    End Sub
    Sub MHide()
        Rows("20:20").Select
        Selection.EntireRow.Hidden = True
        Rows("31:31").Select
        Selection.EntireRow.Hidden = True
        Rows("42:42").Select
        Selection.EntireRow.Hidden = True
        Rows("53:53").Select
        Selection.EntireRow.Hidden = True
        Range("B2").Select
    End Sub
    Sub MShow()
        Rows("20:20").Select
        Selection.EntireRow.Hidden = False
        Rows("31:31").Select
        Selection.EntireRow.Hidden = False
        Rows("42:42").Select
        Selection.EntireRow.Hidden = False
        Rows("53:53").Select
        Selection.EntireRow.Hidden = False
        Range("B2").Select
    End Sub
    Sub MUCT6()
        If Range("g3").Value = "Y" Then
             Call UCT6MShow
             Else: Call UCT6MHide
        End If
    End Sub
    Sub UCT6MHide()
        Rows("19:19").Select
        Selection.EntireRow.Hidden = True
        Rows("30:30").Select
        Selection.EntireRow.Hidden = True
        Rows("41:41").Select
        Selection.EntireRow.Hidden = True
        Rows("52:52").Select
        Selection.EntireRow.Hidden = True
        Range("B2").Select
    End Sub
    Sub UCT6MShow()
        Rows("19:19").Select
        Selection.EntireRow.Hidden = False
        Rows("30:30").Select
        Selection.EntireRow.Hidden = False
        Rows("41:41").Select
        Selection.EntireRow.Hidden = False
        Rows("52:52").Select
        Selection.EntireRow.Hidden = False
        Range("B2").Select
    End Sub

我正在使用动态工作表来填充个性化的付款计划,该工作表基于页面顶部的数据验证下拉列表中的选择。下拉列表中大约有300个选项。然后打印这些时间表,以验证从其他2个程序获得的信息,所有这些程序必须在一天内打印,复制,扫描,打包和邮寄。

我正在寻找可以从列表的开头到结尾依次从下拉列表中选择每个客户端名称的VBA代码。该列表从另一个工作表上的命名范围填充,名为“QtrlyList。”

我有一些非常简单的代码,但不起作用。

    Sub PrintAll()
        For Each cell In QtrlyList
            Worksheets("Normal").PrintOut
        Next cell
    End Sub

每当我尝试运行代码时,都会出现“类型不匹配”错误。我很确定这是来自“cell”或“QtrlyList”。我只是不确定如何解决它。

1 个答案:

答案 0 :(得分:1)

这样的事可能适合你(未经测试)

Sub PrintAll()

    Dim wb as Workbook, cell as Range

    Set Wb = ActiveWorkbook 'or ThisWorkBook if the code is in your reporting workbook 

    For Each cell In wb.Sheets("SheetNameHere").Range("QtrlyList").Cells
        With wb.Worksheets("Normal")
            'you want to set the value of whichever cell has the drop-down
           .Range("D2")).value=cell.Value
           DoEvents 'allow sheet to pick up changed value
           .PrintOut
        End with
    Next cell

End Sub