打印前检查单元格是否包含数据 - 运行-时间错误
Checking cells contain data before printing - Run-Time error
我一直在为工作做一些电子表格工作,到目前为止,我已经能够解决网上搜索的问题。我是 VBA 的新手,大部分内容都是通过在线研究构建的,因此这可能是一种非常垃圾的方式。不幸的是,我 运行 遇到了以下代码的问题,它给我一个类型不匹配错误 (运行-time 13)。我怀疑该代码是不言自明的,但是我实际上是在尝试在某些单元格未完成的情况下禁用打印。 运行-time 错误突出显示了粗体部分。
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim Response As Integer
With Sheets("Settlement Instruction")
**If .Range("M1").Value = "" Or .Range("P1").Value = "" _
Or .Range("E2").Value = "" Or .Range("E3").Value = "" Or .Range("E4").Value = "" Or .Range("M4").Value = "" Or .Range("U3").Value = "" _
Or (.Range("M1").Value = "WA" And .Range("P1").Value = "Settlement" And .Range("T22").Value = "") _
Or (.Range("P1").Value = "Outsource" And (.Range("C32").Value = "" Or .Range("H37").Value = "" Or .Range("C44").Value = "")) _
Or (.Range("P1").Value = "Settlement" And (.Range("G6").Value = "" Or .Range("F8").Value = "" Or .Range("N8").Value = "" Or .Range("F9").Value = "" Or .Range("C32").Value = "" Or .Range("Q35").Value = "" Or .Range("Q36").Value = "" Or .Range("H37").Value = "" Or .Range("F41").Value = "" Or .Range("C44").Value = "")) _
Or (.Range("P1").Value = "Rego Only" Or "Prep Only" And (.Range("F16").Value = "" Or .Range("F41").Value = "" Or .Range("C44").Value = "")) Then**
Response = MsgBox("Please complete all mandatory fields marked in YELLOW.", vbOK, "Information Required")
If Response = vbOK Then
Cancel = True
ElseIf Response = vbCancel Then
Cancel = True
End If
End If
End With
在此之前,我使用了以下代码(除了一些检查不正确之外):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim Response As Integer
With Sheets("Settlement Instruction")
If (.Range("M1").Value = "" Or .Range("E2").Value = "" Or .Range("P1").Value = "" Or .Range("E3").Value = "" Or .Range("E4").Value = "" Or .Range("M4").Value = "" Or .Range("U3").Value = "" Or .Range("C32").Value = "" Or (.Range("P1").Value <> "Outsource" And .Range("F41").Value = "" And .Range("Q35").Value = "" And .Range("Q36").Value = "") Or .Range("C44").Value = "" Or .Range("H37").Value = "" Or (.Range("P1").Value = "Settlement" And (.Range("G6").Value = "" Or .Range("F8").Value = "" Or .Range("N8").Value = "" Or .Range("F9").Value = "")) Or (.Range("P1").Value = "Settlement" And .Range("M1").Value = "WA" And .Range("T22") = "")) Then
Response = MsgBox("Please complete all mandatory fields marke in YELLOW.", vbOK, "Information Required")
If Response = vbOK Then
Cancel = True
ElseIf Response = vbCancel Then
Cancel = True
End If
End If
End With
部分更改是为了使代码更易于使用而不是在同一行上,但是我需要对已完成的检查进行一些更改。
我主要在做一件事。是否有人能够看到导致 运行 时间错误的原因。我确实检查了微软的信息,我认为它是(由于时间限制,我已经看了几天了),我唯一可以推断的是,也许我正在检查文本而不是整数的值。然后我将仅有的 2 个整数字段的格式更改为文本,但仍然有同样的问题。
如有任何帮助,我们将不胜感激。
您因此而收到错误
.Range("P1").Value = "Rego Only" Or "Prep Only"
不能这样用
改为
.Range("P1").Value = "Rego Only" Or .Range("P1").Value = "Prep Only"
我一直在为工作做一些电子表格工作,到目前为止,我已经能够解决网上搜索的问题。我是 VBA 的新手,大部分内容都是通过在线研究构建的,因此这可能是一种非常垃圾的方式。不幸的是,我 运行 遇到了以下代码的问题,它给我一个类型不匹配错误 (运行-time 13)。我怀疑该代码是不言自明的,但是我实际上是在尝试在某些单元格未完成的情况下禁用打印。 运行-time 错误突出显示了粗体部分。
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim Response As Integer
With Sheets("Settlement Instruction")
**If .Range("M1").Value = "" Or .Range("P1").Value = "" _
Or .Range("E2").Value = "" Or .Range("E3").Value = "" Or .Range("E4").Value = "" Or .Range("M4").Value = "" Or .Range("U3").Value = "" _
Or (.Range("M1").Value = "WA" And .Range("P1").Value = "Settlement" And .Range("T22").Value = "") _
Or (.Range("P1").Value = "Outsource" And (.Range("C32").Value = "" Or .Range("H37").Value = "" Or .Range("C44").Value = "")) _
Or (.Range("P1").Value = "Settlement" And (.Range("G6").Value = "" Or .Range("F8").Value = "" Or .Range("N8").Value = "" Or .Range("F9").Value = "" Or .Range("C32").Value = "" Or .Range("Q35").Value = "" Or .Range("Q36").Value = "" Or .Range("H37").Value = "" Or .Range("F41").Value = "" Or .Range("C44").Value = "")) _
Or (.Range("P1").Value = "Rego Only" Or "Prep Only" And (.Range("F16").Value = "" Or .Range("F41").Value = "" Or .Range("C44").Value = "")) Then**
Response = MsgBox("Please complete all mandatory fields marked in YELLOW.", vbOK, "Information Required")
If Response = vbOK Then
Cancel = True
ElseIf Response = vbCancel Then
Cancel = True
End If
End If
End With
在此之前,我使用了以下代码(除了一些检查不正确之外):
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim Response As Integer
With Sheets("Settlement Instruction")
If (.Range("M1").Value = "" Or .Range("E2").Value = "" Or .Range("P1").Value = "" Or .Range("E3").Value = "" Or .Range("E4").Value = "" Or .Range("M4").Value = "" Or .Range("U3").Value = "" Or .Range("C32").Value = "" Or (.Range("P1").Value <> "Outsource" And .Range("F41").Value = "" And .Range("Q35").Value = "" And .Range("Q36").Value = "") Or .Range("C44").Value = "" Or .Range("H37").Value = "" Or (.Range("P1").Value = "Settlement" And (.Range("G6").Value = "" Or .Range("F8").Value = "" Or .Range("N8").Value = "" Or .Range("F9").Value = "")) Or (.Range("P1").Value = "Settlement" And .Range("M1").Value = "WA" And .Range("T22") = "")) Then
Response = MsgBox("Please complete all mandatory fields marke in YELLOW.", vbOK, "Information Required")
If Response = vbOK Then
Cancel = True
ElseIf Response = vbCancel Then
Cancel = True
End If
End If
End With
部分更改是为了使代码更易于使用而不是在同一行上,但是我需要对已完成的检查进行一些更改。 我主要在做一件事。是否有人能够看到导致 运行 时间错误的原因。我确实检查了微软的信息,我认为它是(由于时间限制,我已经看了几天了),我唯一可以推断的是,也许我正在检查文本而不是整数的值。然后我将仅有的 2 个整数字段的格式更改为文本,但仍然有同样的问题。
如有任何帮助,我们将不胜感激。
您因此而收到错误
.Range("P1").Value = "Rego Only" Or "Prep Only"
不能这样用
改为
.Range("P1").Value = "Rego Only" Or .Range("P1").Value = "Prep Only"