Solved

Looping problem

Posted on 2013-10-22
6
128 Views
Last Modified: 2013-10-22
Folks,
The code below is failing me. Here's my objective. In the Range D5:E12 if the value is "Correct" then I execute a module "FormulasOK". If a value <> "Correct" then I execute a module "CheckFormulaFunction".

Dim lRowLoop As Long

For lRowLoop = 5 To 12

 If Cells(lRowLoop, 4).Text = "Correct" Then
     Next lRowLoop
     Else
        CheckFormulaFunction
        Exit Sub
    End If

Open in new window

0
Comment
Question by:Frank Freese
  • 3
  • 2
6 Comments
 
LVL 24

Assisted Solution

by:Steve
Steve earned 100 total points
ID: 39591177
try:

Dim lRowLoop As Long
For lRowLoop = 5 To 12
 If Cells(lRowLoop, 4).value = "Correct" Then
        call FormulasOK
     Else
        call CheckFormulaFunction
    End If 
 Next lRowLoop

Open in new window

The above code only loops through D5:D12
If you want E5 to E12 you would repeat using Cells(lRowLoop, 5).value

you will need subs or functions called FormulasOK and CheckFormulaFunction
but this should sort out the code logic for you.
0
 
LVL 81

Accepted Solution

by:
byundt earned 400 total points
ID: 39591189
Did you mean to call FormulasOK only if all cells are Correct?
Sub FormulaChecker()
Dim lRowLoop As Long
For lRowLoop = 5 To 12
    If Cells(lRowLoop, 4).Text <> "Correct" Then
        CheckFormulaFunction
        Exit Sub
    End If
Next
FormulasOK
End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:Steve
Steve earned 100 total points
ID: 39591212
Ahh, Brad, you may be right there, in that case I would tend to use a Boolean...

Sub FormulaChecker()
Dim lRowLoop As Long
Dim bCorrect as Boolean: bCorrect = True

For lRowLoop = 5 To 12
    If Cells(lRowLoop, 4).Value <> "Correct" Then bCorrect = False
Next
        
If bCorrect then
    FormulasOK
else
    CheckFormulaFunction
end if

End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Frank Freese
ID: 39591341
I've changed Brad's suggestion some. As I read my changes below, what happens is that the first Loop checks for the "Correct" from 5 - 12 column 4. If there's not a "Correct" then CheckFormulaFunction is executed and I execute the sub.
The same thing happens in the seond Loop except it checks column 5. If everything is "Correct" then it executes FormulasOK. Is this new code correct then?

Dim lRowLoop As Long
For lRowLoop = 5 To 12
 If Cells(lRowLoop, 4).value = "Correct" Then
             Else
             CheckFormulaFunction
             Exit sub
    End If 
 Next lRowLoop

For lRowLoop = 5 To 12
 If Cells(lRowLoop, 5).value = "Correct" Then
             Else
             CheckFormulaFunction
             Exit Sub
    End If 
 Next lRowLoop

FormulasOK

Open in new window

0
 

Author Comment

by:Frank Freese
ID: 39591411
The revised code I posted did what I was looking for.
With a few changes to Brads code I felt like he deserved the majority of the points.
But I am grateful for all that chimed in.
0
 

Author Closing Comment

by:Frank Freese
ID: 39591419
thanks everyone.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find word and 6 digit number 22 95
Macro Capitalize 4 51
Display the VBA Userform in Screen Center 4 30
Excel Score Formula 5 46
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now