![](/img/trans.png)
[英]can someone tell me what's wrong with my program using fork() system calls(Programming C)
[英]Can someone tell me what is wrong with my 'MsgBox' code?
我在此编码上遇到编译错误,无法弄清楚红色行有什么问题?
我已经搜索了几个站点以确定可能出了什么问题,但没有找到任何可以回答我的问题的内容?
Sub MsgBoxCritical()
Dim ws As Worksheet
Set ws = Worksheets("Travel Expense Form")
Dim amt As Range
Set amt = Range("U15:U45")
Dim proj As Range
Set proj = Range("N15:N45")
For Each Cell In ws("amt")
If Cell.Value > 0 Then
For Each Cell In ws("proj")
If Cell.Value = "" Then Cell.Interior.Color = vbRed
MsgBox "Project Number must be provided for all lines where
reimbursement is being requested" & vbCritical
Cancel = True
End If
End Sub
如果 U 列第 15-45 行中的任何单元格大于 0 并且相应行中 N 列中的单元格为空白,我希望在保存工作簿时显示此消息框。
我收到的编译错误出现在 Range U15:U45 的行上,并且是Expected:Expression
错误?
编译错误意味着 VBA 无法编译代码。 因此,它突出了“奇怪”的行。 在这种情况下,两个 If 条件有点错误。 这是编写And
的标准方式。 它写成 1 如果:
Sub TestMe()
Dim conditionA As Boolean
Dim conditionB As Boolean
conditionA = True
conditionB = True
If conditionA And conditionB Then
MsgBox "Both true!"
End If
End Sub
关于代码,它有一些缺陷。 通常,如果应检查范围内的每个单元格,则 go 循环并检查它。 在某些情况下,也可以尝试WorksheetFunction.Sum(Worksheets("Travel Expense Voucher").Range("U15:U45"))>0
,但是很难获得高于 0 的行这个案例。 反正:
Sub MsgBoxCriticalIcon()
Dim myCell As Range
With Worksheets("Travel Expense Voucher")
For Each myCell In .Range("U15:U45")
If myCell.Value > 0 And .Cells(myCell.Row, "N") = "" Then
MsgBox "Project must be ... at row " & myCell.Row
Exit Sub
End If
Next myCell
End With
End Sub
发布的 MsgBox 代码有两个问题:
If Cell.Value = "" Then Cell.Interior.Color = vbRed
MsgBox "Project Number must be provided for all lines where
reimbursement is being requested" & vbCritical
Cancel = True
(将周围的两行代码留在上下文中)
首先是vbCritical
是MsgBox
调用的标志和单独参数。 但是,这可能会在字符串末尾放置一个不寻常的数字。
第二个问题是您的文本行已经环绕并显示在单独的行上。 这会导致编译错误。
尝试这个:
If Cell.Value = "" Then Cell.Interior.Color = vbRed
MsgBox "Project Number must be provided for all lines where reimbursement is being requested", vbCritical
Cancel = True
请注意,正确的缩进将帮助您识别代码块并确保您的If
语句正确匹配。 另外,不确定Cancel
在这个循环中做了什么,但了解 scope 以及如何正确传递变量(作为参数或通过函数返回它们)。
最后,始终在模块顶部使用Option Explicit
。 虽然这可以确保在您有未声明的变量时进行强类型化并返回错误(对于找出拼写错误非常有用),但在这种情况下,它还将确保 VBA IDE 在调试时提供一些额外的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.