excel VBA中的错误451(属性let过程未定义...)

时间:2018-06-16 00:30:15

标签: excel vba excel-vba

我一直在努力使用这段代码,我需要一些帮助。 Disclamer,我非常新,因此编程非常糟糕,所以如果你看到一些优化要做,请随意撕开我的代码。

所以,我想根据某些单元格有条件地打印实验报告的某些特定部分。因此,如果单元格的值不是0,则将打印整个行或部分。

我发现这个Union功能允许我添加打印区域,所以我决定将AreaImp变暗作为一个变量,并继续添加一块原始图块,所以最后它将被打印出来。

问题在于,当我尝试运行代码时,它会在PrintOut行中获取错误451(Property Let过程未定义且属性Get过程未返回对象)(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

无论如何,提前感谢,如果您的眼睛因为阅读本书而流泪,那就很抱歉

1 个答案:

答案 0 :(得分:0)

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

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

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