【问题标题】:Error 451 in excel VBA (Property let procedure not defined...)excel VBA中的错误451(未定义属性让程序...)
【发布时间】:2018-06-16 00:30:14
【问题描述】:

我已经为这段代码苦苦挣扎了几天,我需要一些帮助。 Disclamer,我是新手,因此在编程方面非常糟糕,所以如果你看到需要做一些优化,请随意撕开我的代码。

所以,问题是我想有条件地打印实验室报告的某些特定部分,具体取决于某些单元格是否具有价值。因此,如果单元格的值不是 0,则将打印整行或整节。

我找到了允许我添加打印区域的 Union 函数,因此我决定将 AreaImp 调暗为变量并继续添加工作表的块,以便最终打印所有内容。

问题是,当我尝试运行代码时,它在 PrintOut 行(End Sub 之前的最后一行代码) 而且我真的不知道在哪里寻找解决方案,因为我不知道是什么原因造成的。

Sub Imprimir()
 Worksheets("Hemato+Bcas").Activate
 Dim i As Integer
 Dim vShts As Variant
 Dim AreaImp As Variant

 AreaImp = ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(12, 6))

 'If hematologia

    vShts = Sheets("Hemato+Bcas").Cells(15, 3)

    If Not IsNumeric(vShts) Then
        Exit Sub
    Else
        If vShts > 0 Then
            AreaImp = ActiveSheet.PageSetup.PrintArea = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(13, 1), ActiveSheet.Cells(51, 6))).Address
        End If
    End If

 'For bioquimicas

    For i = 56 To 73

        vShts = Sheets("Hemato+Bcas").Cells(i, 3)

        If Not IsNumeric(vShts) Then
            Exit Sub
        Else
            If vShts > 0 Then
                AreaImp = ActiveSheet.PageSetup.PrintArea = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(52, 1), ActiveSheet.Cells(55, 6))).Address
                AreaImp = ActiveSheet.PageSetup.PrintArea = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(i, 1), ActiveSheet.Cells(i, 6))).Address
                AreaImp = ActiveSheet.PageSetup.PrintArea = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(74, 1), ActiveSheet.Cells(86, 6))).Address
            End If
        End If
    Next i

    ActiveSheet.PageSetup.PrintArea(AreaImp).PrintOut

End Sub

无论如何,提前致谢,如果您阅读本文时眼睛流血,我们深表歉意

【问题讨论】:

  • 看起来 AreaImp 应该是一个范围并设置为工作表范围。另外,我不清楚 AreaImp = ActiveSheet.PageSetup.PrintArea = Union(... 应该做什么。

标签: vba excel


【解决方案1】:

联合是连续或非连续单元格的合并。您设置第一个范围,然后设置为第一个范围和第二个范围的并集。

当您开始分配 .PageSetup.PrintArea 时,请使用联合的地址,而不是联合作为范围对象。

Sub Imprimir()
    Dim i As Integer
    Dim vShts As Variant
    Dim AreaImp As Variant


 'If hematologia

    vShts = Worksheets("Hemato+Bcas").Cells(15, 3).Value

    If Not IsNumeric(vShts) Then
        Exit Sub
    Else
        Worksheets("Hemato+Bcas").Activate
        Set AreaImp = ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(12, 6))
        If vShts > 0 Then
            Set AreaImp = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(13, 1), ActiveSheet.Cells(51, 6)))
        End If
    End If

 'For bioquimicas

    For i = 56 To 73

        vShts = Worksheets("Hemato+Bcas").Cells(i, 3).Value
        Set AreaImp = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(52, 1), ActiveSheet.Cells(55, 6)))
        Set AreaImp = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(74, 1), ActiveSheet.Cells(86, 6)))

        If Not IsNumeric(vShts) Then
            Exit Sub
        Else
            If vShts > 0 Then
                Set AreaImp = Union(AreaImp, ActiveSheet.Range(ActiveSheet.Cells(i, 1), ActiveSheet.Cells(i, 6)))
            End If
        End If
    Next i

    ActiveSheet.PageSetup.PrintArea = AreaImp.Address
    ActiveSheet.PrintOut

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    相关资源
    最近更新 更多