'Next without for' compile error

KristenB

New Member
Joined
Nov 9, 2019
Messages
21
Hello - I have the following macro and it is hanging up with a "next without for" compile error. I don't see what is wrong with it? Could someone please help me? It is hanging up on the line 'Next mycell".

Code:
Public Sub ProjNumbrReq()
'Call ProjNumbrReq
Dim myCell As Range
With Worksheets("Travel Expense Voucher")
  If .Range("F5") = 2 Then
    For Each myCell In .Range("W16:W46")
        If myCell.Value > 0 And .Cells(myCell.row, "N") = "" Then
          MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbCritical, "Important:"
          Exit Sub
     Next myCell
  End If
 End With
End Sub
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You were missing an 'end if'. This is why indentation is your friend.

Code:
Public Sub ProjNumbrReq()
'Call ProjNumbrReq
Dim myCell As Range
With Worksheets("Travel Expense Voucher")
    If .Range("F5") = 2 Then
        For Each myCell In .Range("W16:W46")
            If myCell.Value > 0 And .Cells(myCell.Row, "N") = "" Then
                MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbCritical, "Important:"
                Exit Sub
            End If
        Next myCell
    End If
End With
End Sub
 
Upvote 0
I updated and now I'm getting a 'Runtime Error 13 Type Mismatch" :( This particular code is going to give me serious gray hair!
 
Upvote 0
In the range of W16:W46 cells do you have formulas? Do any of these formulas return error?
 
Upvote 0
You know what? I am working on a few other formulas on that worksheet and some of the cells in that range have a #value error on them. Will that cause the error that I'm getting? I'm new to this coding thing so please forgive me if that is a stupid question. :(
 
Upvote 0
Do not worry, we are all learning.
Yes, the error in the cell is the cause of the error in the code, but you can try the following:

Code:
Public Sub ProjNumbrReq()
'Call ProjNumbrReq
  Dim myCell As Range
  With Worksheets("Travel Expense Voucher")
    If .Range("F5") = 2 Then
        For Each myCell In .Range("W16:W46")
          If Not IsError(myCell) Then
            If myCell.Value > 0 And .Cells(myCell.Row, "N") = "" Then
                MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbCritical, "Important:"
                Exit Sub
            End If
          End If
        Next myCell
    End If
  End With
End Sub
 
Upvote 0
Perfect!!! Thank you! Now if I can just figure out the rest of this. I'm also trying to add userforms and some of that coding is challenging. Additionally, I'm also trying to figure out how to calculate time, determining if there was at least 3 hours between times, to create errors when someone is trying to claim a reimbursement for a time frame that is not allowed. Sound fun? :) Thank you so much for your help!!
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top