vba - excel VBA中的错误451(未定义属性让程序...)
问题描述
我已经为这段代码苦苦挣扎了几天,我需要一些帮助。Disclamer,我很新,因此在编程方面非常糟糕,所以如果你看到需要做一些优化,请随意拆开我的代码。
所以,问题是我想有条件地打印实验室报告的某些特定部分,具体取决于某些单元格是否具有价值。因此,如果单元格的值不是 0,则将打印整个行或部分。
我发现这个 Union 函数允许我添加打印区域,所以我决定将 AreaImp 调暗为变量并继续添加工作表的块,以便最后打印所有内容。
问题是,当我尝试运行代码时,它在 PrintOut 行(End Sub 之前的最后一行代码)中出现错误 451(Property Let 过程未定义且 property Get 过程未返回对象),我老实说,我不知道在哪里寻找解决方案,因为我不知道是什么原因造成的。
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
无论如何,提前谢谢,如果你读到这里流血了,对不起
解决方案
联合是连续或非连续单元格的合并。您设置第一个范围,然后设置为第一个范围和第二个范围的并集。
当您开始分配 .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
推荐阅读
- javascript - 嵌套的 setTimeout 方法如何按顺序执行?
- r - 如何为 R Shiny 中的可扩展矩阵自动生成顺序列标题?
- java - 为什么同一组中的 kafka-0.9 多个消费者使用相同的消息
- python - 如何将照片上传到 MySQL 并浏览
- google-chrome-extension - Chrome 扩展和第三方 cookie 替代方案
- file - 如何打开带有 .zvX 文件扩展名的文件?
- postgresql - 如果 nickName 不为 null,则按 nickName 排序,否则按 firstName 排序
- api - Facebook API - 发布到页面不起作用
- android - Android AnimatorSet.cancel() 不适用于 Marshmallow 版本的设备
- .net - 有条件地包含基于路径的项目