'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:

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,588
Office Version
365, 2019, 2016
Platform
Windows
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
 

KristenB

New Member
Joined
Nov 9, 2019
Messages
21
I updated and now I'm getting a 'Runtime Error 13 Type Mismatch" :( This particular code is going to give me serious gray hair!
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,588
Office Version
365, 2019, 2016
Platform
Windows
What line is the error being thrown on?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,881
Office Version
2007
Platform
Windows
In the range of W16:W46 cells do you have formulas? Do any of these formulas return error?
 

KristenB

New Member
Joined
Nov 9, 2019
Messages
21
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. :(
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,881
Office Version
2007
Platform
Windows
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
 

KristenB

New Member
Joined
Nov 9, 2019
Messages
21
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!!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,881
Office Version
2007
Platform
Windows
Im glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,082,246
Messages
5,363,995
Members
400,772
Latest member
solbebe

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top