Reputation: 87
I am getting a Compile Error on this coding and can't figure out what is wrong with the line in red?
I have searched several sites to determine what might be wrong but haven't found anything that answers my issue?
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
I am looking to present this message box when the workbook is saved if any cell in Column U, rows 15-45 are greater than 0 AND if the cell in Column N in the corresponding row is blank.
The compilation error I am receiving is on the line for Range U15:U45 and is an Expected:Expression
error?
Upvotes: 1
Views: 59
Reputation: 2438
There are two concerns with the MsgBox code as posted:
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
(Left the two surrounding lines of code in for context)
The first is that vbCritical
is a flag and a separate parameter to the MsgBox
call. However, that would probably jsut put an unusual number at the end of the string.
The second problem is that your text lines have wrapped around and are presented on separate lines. This would cause a compile error.
Try this:
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
Note that proper indentation will help you identify blocks of code and ensure that your If
statements are properly matched. ALso, not sure what Cancel
is doing in this loop, but learn about scope and how to properly pass variables (either as parameters or return them through functions).
Finally, always use Option Explicit
at the top of the module. While this ensures strong typing and returns errors when you have undeclared variables (very useful for picking up typos), in this case it will also ensure the VBA IDE provides some additional information when debugging.
Upvotes: 1
Reputation: 43575
Compile error means, that VBA cannot compile the code. Thus, it highlights the line that is "strange". In this case, the two If conditions are a bit wrong. This is the standard way to write And
. It is written with 1 If:
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
Concerning the code, there are some flaws in it. In general, if every cell from a range should be checked, then go for a loop and check it. In some cases, it is also possible to try WorksheetFunction.Sum(Worksheets("Travel Expense Voucher").Range("U15:U45"))>0
, but it would be tough to get the row, which is above 0 in this case. Anyway:
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
Upvotes: 1