【问题标题】:How to use For/Next loops to print range based on check box status如何使用 For/Next 循环根据复选框状态打印范围
【发布时间】:2015-08-19 05:12:09
【问题描述】:

我无法将一些代码放在一起,这些代码允许我查看复选框的状态并在复选框等于 true 时在另一个工作表上打印指定范围,然后针对给定数量的复选框重复该循环,并且范围。

我正在使用两个不同的 For - Next 循环来尝试完成这项任务。以下是我当前的代码。

Sub PrintGraphs()
Dim i As Integer
Dim r As Integer
    For i = 16 To 18
        If ActiveSheet.OLEObjects("CheckBox" & i).Object.Value = True Then
            For r = 1 To 3
                Worksheets("Cut Tables Graphs").Activate
                    Range("RangeSet" & r).Select
                    ActiveSheet.PageSetup.PrintArea = ("RangeSet" & r)
                    With ActiveSheet.PageSetup
                        .PaperSize = xlPaperLetter
                        .Orientation = xlLandscape
                        .Zoom = False
                        .FitToPagesWide = 1
                        .FitToPagesTall = 1
                        .LeftMargin = Application.InchesToPoints(0.25)
                        .RightMargin = Application.InchesToPoints(0.25)
                        .TopMargin = Application.InchesToPoints(0.25)
                        .BottomMargin = Application.InchesToPoints(0.25)
                        .CenterHorizontally = True
                        .CenterVertically = True
                    End With
                    Application.PrintCommunication = True
                    Selection.PrintOut Copies:=1, Collate:=True
            Next r
        End If
    Next i
End Sub

我还在一个单独的模块上使用公共常量来定义变量“RangeSet”。该模块上的代码如下。

Public Const RangeSet1 = "B2:U51"
Public Const RangeSet2 = "W2:AP51"
Public Const RangeSet3 = "B53:U102"

我希望我的代码查看复选框 16 到 18,然后打印复选框值等于 true 的相应范围。

期望的结果:

如果复选框 #16 等于 true,则打印范围“B2:U51”,否则 Next i

如果复选框 #17 等于 true,则打印范围“W2:AP51”,否则 Next i

如果复选框 #18 为真,则打印范围“B53:U102”,否则结束循环

我目前遇到的错误是:运行时错误'1004':在包含“Range("RangeSet" & r).Select 的行上的调用目标已引发异常。选择

我们将不胜感激任何有关找到解决此问题的方法的帮助。提前谢谢你。

【问题讨论】:

    标签: vba excel for-loop next


    【解决方案1】:

    我觉得最简单的方法是创建一个包含您要记录的值的数组。填充完数组后,您可以使用 for 循环将其打印到您想要的任何位置。

    【讨论】:

    • 我是 Excel VBA 的新手,您是否介意详细说明您的评论,或者提供一个我可以逆向工程的示例。
    【解决方案2】:
    Range("RangeSet" & r).Select
    

    不是 VBA 中的有效范围。你将把它传递给“RangeSet1”,因为它不是一个范围,也不是你声明的变量。

    可以试试

    if r = 1 then
         Range(RangeSet1).Select
    elseif r=2 then
         Range(RangeSet2).Select
    elseif r=3 then
         Range(RangeSet3).Select
    else
        stop'should never get here?
    endif
    

    有一种方法可以在一行代码中做到这一点,但我不记得它的语法了。

    【讨论】:

    • 在一行代码中应该有相同的结果:Range(Choose(r, RangeSet1, RangeSet2, RangeSet3)).Select
    • 我能够实现单行代码来选择变量“r”,但它不会将“r”的选择与适当的变量“i”选择联系起来。它似乎只是打印变量“r”的所有范围。例如,如果只有复选框 17 = true 和 16 & 18 = false,它仍然会打印所有三个范围,而不仅仅是复选框 17 的范围。@DarrenBartrup-Cook
    • 我能够得到代码以打印出正确的范围,基于使用'If i = 16 Then r = 1 / Elseif i = 17 Then r = 2 / Elseif 检查的复选框i = 18 Then r = 3 / End If' 语句以及 '(Choose(r, RangeSet1, RangeSet2, RangeSet3)' 代码。如果未选中最后一个复选框,则会出现以下错误:运行时错误“1004”:无法获取 Wroksheet 类的 OLEObject 属性。如果选中了最后一个复选框,我不会收到错误消息。关于可能导致此问题的任何想法??
    • 使用上面的代码,您应该能够用我的示例替换“Range("RangeSet" & r).Select”,其余代码应该可以正常工作。如果您愿意,可以将 r=1 更改为复选框 #16.checked = true 等
    【解决方案3】:

    这就是我编写代码的方式

    Sub PrintGraphs()
    
        Dim i As Integer
    
        With ThisWorkbook.Worksheets("Cut Tables Graphs")
            With .PageSetup
                .PaperSize = xlPaperLetter
                .Orientation = xlLandscape
                .Zoom = False
                .FitToPagesWide = 1
                .FitToPagesTall = 1
                .LeftMargin = Application.InchesToPoints(0.25)
                .RightMargin = Application.InchesToPoints(0.25)
                .TopMargin = Application.InchesToPoints(0.25)
                .BottomMargin = Application.InchesToPoints(0.25)
                .CenterHorizontally = True
                .CenterVertically = True
            End With
            Application.PrintCommunication = True
            For i = 16 To 18
                If .OLEObjects("CheckBox" & i).Object.Value = True Then
    
                    .Range(Choose(i - 15, RangeSet1, RangeSet2, RangeSet3)).PrintOut Copies:=1, Collate:=True
    
                End If
            Next i
        End With
    
    End Sub
    

    不需要变量 r - 如果您选择 CheckBox16,您想打印范围 1 - 因此从 i 中删除 15 并得到 1 - 选择该范围(希望有意义)。

    我还假设“ActiveSheet”(带有复选框的那个)是“Cut Tables Graphs”。

    我在底部包含了您的原始代码和一些 cmets。

    Sub PrintGraphs()
    Dim i As Integer
    Dim r As Integer
        'This will work - providing the correct sheet is active.
        For i = 16 To 18
            If ActiveSheet.OLEObjects("CheckBox" & i).Object.Value = True Then
    
                'This will cycle through all three no matter which checkbox was ticked.
                For r = 1 To 3
    
                    'No need to activate the sheet.  Especially if it's the ActiveSheet.
                    Worksheets("Cut Tables Graphs").Activate
    
                        'No need to select a range before working on it.
                        Range("RangeSet" & r).Select
    
                        'This only needs doing once - not at all if it's the default anyway.
                        'As it's inside the 1 to 3 loop it will happen 3 times.
                        ActiveSheet.PageSetup.PrintArea = ("RangeSet" & r)
                        With ActiveSheet.PageSetup
                            .PaperSize = xlPaperLetter
                            .Orientation = xlLandscape
                            .Zoom = False
                            .FitToPagesWide = 1
                            .FitToPagesTall = 1
                            .LeftMargin = Application.InchesToPoints(0.25)
                            .RightMargin = Application.InchesToPoints(0.25)
                            .TopMargin = Application.InchesToPoints(0.25)
                            .BottomMargin = Application.InchesToPoints(0.25)
                            .CenterHorizontally = True
                            .CenterVertically = True
                        End With
                        Application.PrintCommunication = True
    
                        'Rather than select the range and print the selection,
                        'just reference the range and print it.
                        Selection.PrintOut Copies:=1, Collate:=True
                Next r
            End If
        Next i
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多