How to evaluate a range of cells in VB?

Closed
Stacey - Nov 12, 2010 at 10:32 AM
 RayH - Nov 23, 2010 at 11:05 AM
Hello,

I am creating a monitoring form. I want it to evaluate the answers for each question. If the Auditor does not select one of the three answers, I want the auditor to get an error message informing them that a question has not been answered and the question to highlight yellow indicating which question was not answered.

This is what I have thus far:

Sub Submit()
Dim Start As String
Dim Rng1 As Range
Dim Prompt As String, RngStr As String
Dim Cell As Range
Set Rng1 = Range("I11:K11")
Start = True
For Each Cell In Rng1
If Cell.Value = "" Then
MsgBox "Audit Form Incompete. Please review highlighted question(s)."
Cell.Interior.ColorIndex = 6 '** color yellow
If Start Then RngStr = RngStr & Cell.Parent.Name & vbCrLf
Start = False
RngStr = RngStr & Cell.Address(False, False) & ", "
Else
Cell.Interior.ColorIndex = X '** no color
End If


Currently, it gives an error message for each individual blank. I want it to evaluate the range as one and only give an error message and turn yellow if all options are blank.

Thanks in advance!!!

8 responses

I've designed this to be as flexiable as possible.
There are two routines. One Sub and one function.

There is a routine that calls it. I've called it 'runit'
The Check_Answers sub goes down thorugh the questions and for each calls a function called 'HighlightRow'. This function checks the responses and determines whether they have made a choice. If not, the row is highlighted yellow otherwise the yellow is removed.

The Check_Answers sub needs FOUR parameters passed to it.
The starting row and starting column, the number of questions and the number of answers.

Dim Err As Boolean
Dim msgflag As Boolean

Sub Check_Answers(RowStart As Long, ColumnStart As Long, NumberofQuestions As Integer, NumberofAnswers As Integer)
Dim myRow As Long
Dim myColumn As Long

Let myRow = RowStart
Let myColumn = ColumnStart

For myRow = myRow To myRow + NumberofQuestions - 1
    Err = HighlightRow(myRow, myColumn, NumberofAnswers)

    If Err And Not msgflag Then Let msgflag = True
Next myRow

End Sub


Function HighlightRow(qRow As Long, qcolumn As Long, NumberofAnswers)
' This function turns the row Yellow if all are left blank

Dim errflag As Integer
Dim testcol As Long
Let errflag = 0

' For each cell in the range, count how many cells are blank
For testcol = qcolumn To qcolumn + NumberofAnswers - 1
  If Cells(qRow, testcol).Value = "" Then Let errflag = errflag + 1
Next testcol

If errflag = NumberofAnswers Then
    ' Turn of row highlight (yellow)
        Range(Cells(qRow, qcolumn), Cells(qRow, qcolumn + NumberofAnswers - 1)).Interior.Color = RGB(255, 255, 0) ' Yellow
        HighlightRow = True
Else
    ' Turn off row highlight
        Range(Cells(qRow, qcolumn), Cells(qRow, qcolumn + NumberofAnswers - 1)).Interior.Pattern = xlNone ' No Fill
        HighlightRow = False
End If
End Function

Sub runit()
Let Err = False
Let msgflag = False

' Check answers starting from row 2 and column 2 for 20 questions with 3 answers each
' Cell B2
Check_Answers 2, 2, 20, 3

' Check answers starting from row 24 and column 2 for 30 questions with 4 answers each
' Cell B24
Check_Answers 24, 2, 30, 4

' Display msg if any of the answers are blanks (covers both sections)
If msgflag Then MsgBox "Audit Form Incompete. Please review highlighted question(s)."

End Sub
1
This works PERFECTLY!!!! Thank you for all your help!!!!!!
0
Define a variable:
DIM errflag as integer

Then in place of the MsgBox statement put:
let errflag=1

Move the MsgBox to the end of the section and change it to be:
If errflag Then MsgBox "Audit Form Incompete. Please review highlighted question(s)."
0
I appreciate the help but I have add the info you have supplied and it doesn't work for me. I really believe I am doing something wrong. Could you please use the following code and highlight or make a note of exactly what I need to change?
Sub Submit()
Dim Start As String
Dim Rng1 As Range
Dim Prompt As String, RngStr As String
Dim Cell As Range
Set Rng1 = Sheets("Auditing_Form").Range("I11:K11")
Start = True
For Each Cell In Rng1
If Cell.Value <> "X" Then
MsgBox "Audit Form Incompete. Please review highlighted question(s)."
Cell.Interior.ColorIndex = 6 '** color yellow
If Start Then RngStr = RngStr & Cell.Parent.Name & vbCrLf
Start = False
RngStr = RngStr & Cell.Address(False, False) & ", "
Else
Cell.Interior.ColorIndex = X '** no color
End If


I really appreciate your help!!!
0
Sub Submit()
Dim Start As String
Dim Rng1 As Range
Dim Prompt As String, RngStr As String
Dim Cell As Range

' This is Change #1
DIM ErrFlag as Integer
Let ErrFlag=0 ' Reset the variable just to be sure

Set Rng1 = Sheets("Auditing_Form").Range("I11:K11")
Start = True
For Each Cell In Rng1
If Cell.Value <> "X" Then

' This is change #2
let ErrFlag=1

Cell.Interior.ColorIndex = 6 '** color yellow
If Start Then RngStr = RngStr & Cell.Parent.Name & vbCrLf
Start = False
RngStr = RngStr & Cell.Address(False, False) & ", "
Else
Cell.Interior.ColorIndex = X '** no color
End If

' This is Change #3
if ErrFlag then MsgBox "Audit Form Incompete. Please review highlighted question(s)."
0

Didn't find the answer you are looking for?

Ask a question
Dont forget to include "Next Cell" afer the the last End If and before the MsgBox
0
That works perfectly, only one problem now. The the blank cells are still turning yellow and the Error Message is still popping up for each cell in the Range I11:K11, except for the one I have marked. Is there any way to only turn the blank cells yellow and only show the error message if all three cells are left blank?

Thanks for your help!!!
0
try replacing let ErrFlag=1 with Let ErrFlag=Errflag+1
Then with the MsgBox command use IF ErrFlag=3 then MsgBox "Audit...
0
That works great.. One last thing and I will be out of your hair :-).
How do I make the cells that turn yellow from the entire row being blank turn back to the normal color once a selection has been made on that row?

Thanks
0
I'm not sure I understand. As it stands once a selection is made, i.e. the cell is no longer blank, the once yellow cell turns back to normal. Blank means blank in this instance not filled with a space.
0
Let give you an example, maybe that will help me explain it to you :-):

I have a question with three possible answers:

If I do not select one of the three answers, the three cells indicating the answer will turn yellow and give me an error message indicating I need to correct the problem.

Once I go back and correct the problem and rerun the Macro, the cell that I select as the answer will turn back white (as it should) but the other two cells will remain yellow.

I need the other to empty cells to also turn back white indicating that one of the cells have been selected as the answer.

I hope this helps you a little better! Thanks
0
So are these 3 cells the possible answers in a multiple choice question? I thought they were each independant questions/answers. Can you provide a little bit more details on the layout. e.g. the question and the possible answers, if this is what you mean.
0
This should satisfy section1.
Section 2 is almost the same except with a different range.

Dim Rng1 As Range
Dim Cell As Range
Dim errflag As Integer

Set Rng1 = Range("I8:K8")
Let errflag = 0

' For each cell in the range, count how many cells are blank
For Each Cell In Rng1
    If Cell.Value = "" Then Let errflag = errflag + 1
Next Cell
 
' If all 3 cells are blank then turn yellow and display message
 If errflag = 3 Then
    Rng1.Interior.Color = RGB(255, 255, 0) ' Yellow
    MsgBox "Audit Form Incompete. Please review highlighted question(s)."

'otherwise turn range to 'No Fill'
 Else
    Rng1.Interior.Pattern = xlNone ' No Fill
 End If
0
Good morning, and thanks for all your help thus far. I have tried the coding you have provided to me but I am still having a problem.

The coding isn't working properly for multiple ranges.
Example: If I use the following coding with the range for one question (I11:K11) the coding works as it should:

Sub Submit()
Dim Rng1 As Range
Dim Cell As Range
Dim errflag As Integer

Set Rng1 = Sheets("Monitoring_Form").Range("I11:K11")

Let errflag = 0

' For each cell in the range, count how many cells are blank
For Each Cell In Rng1
If Cell.Value = "" Then Let errflag = errflag + 1
Next Cell

' If all 3 cells are blank then turn yellow and display message
If errflag = 3 Then
Rng1.Interior.Color = RGB(255, 255, 0) ' Yellow
MsgBox "Audit Form Incompete. Please review highlighted question(s)."

'otherwise turn range to 'No Fill'
Else
Rng1.Interior.Pattern = xlNone ' No Fill
End If

End Sub


but if I try to add multiple questions using a broader range such as I11:K16 the coding does not work appropriately. I changed the errflag=3 to errflag=18. I would get the error message but if I answered one of the questions the remaining questions would also turn back to the regular color. Meaning that the coding is recognizing the ranges as one. Below is the coding I used trying to add multiple ranges:

Dim Rng1 As Range
Dim Cell As Range
Dim errflag As Integer

Set Rng1 = Sheets("Monitoring_Form").Range("I11:K16,I19:K24,I27:K34,H40:K44,H47:K51")

Let errflag = 0

' For each cell in the range, count how many cells are blank
For Each Cell In Rng1
If Cell.Value = "" Then Let errflag = errflag + 1
Next Cell

' If all 3 cells are blank then turn yellow and display message
If errflag = 18 Then
Rng1.Interior.Color = RGB(255, 255, 0) ' Yellow
MsgBox "Audit Form Incompete. Please review highlighted question(s)."

'otherwise turn range to 'No Fill'
Else
Rng1.Interior.Pattern = xlNone ' No Fill
End If


Keep in mind this is only for section 1 with the 3 answers provided.

Thanks for all your help!
0
I would expect the code to work. The orignal scope was for just those 3 cells. Is this the extent of the changes or should there be anything else I'd need to know about?
0
I'm so sorry, i guess I should have told you that early on.

The form consist of 30 questions:
1-20 have the 3 options YES, NO, NA
21-30 have the 4 options 0, 1, 2, NA

I need a Macro that will evaluate each individual question. If the question (answer options) are left blank, then the cells should turn yellow and give the error message when the Macro is ran.

Once the blank question has been updated and the Macro ran again, the cells that turned yellow should return to the original color indicating the question has been answered.

I hope I explained it to you better this time.

Thanks in advance!
0
I have just one more question...

I have a form that is set to Manual Calculation b/c I have a "submit" button to evaluate the form and also Calculate it. When I select the cell that VLOOKUP runs, the VLOOKUP cells does not populate (b/c automatic calculations is turned off) Once I hit submit, the VLOOKUP cell populates.

When I use the reset button I created, the VLOOKUP cell does not clear the data, I have to hit the submit button again in order to clear the VLOOKUP cell.

Is there a Macro I can add to my "reset" macro to clear that VLOOKUP cell (without deleting the formula) to set the cell back to a blank cell?

Thank you for your help in advance!!!
0
Stacey/LearningVB
Have you tried inserting the 'Calculate' command in the macro?
Without seeing the 'reset' code its a little hard to tell what you're doing.
0
Thanks for your help!

This is the Calculation Code I added to the "Runit" Macro you created for me:

Dim wks As Worksheet
Application.Calculation = xlManual
For Each wks In ActiveWorkbook.Worksheets
wks.Calculate
Next
Set wks = Nothing


This is the reset code that I have:

Sub Refresh()
'
' Refresh Macro
'

'
Range("I11:K16").Select
ActiveWindow.SmallScroll Down:=12
Range("I11:K16,I19:K24").Select
Range("I19").Activate
ActiveWindow.SmallScroll Down:=9
Range("I11:K16,I19:K24,I27:K34").Select
Range("I27").Activate
ActiveWindow.SmallScroll Down:=12
Range("I11:K16,I19:K24,I27:K34,H40:K44").Select
Range("H40").Activate
ActiveWindow.SmallScroll Down:=9
Range("I11:K16,I19:K24,I27:K34,H40:K44,H47:K51,A56:K64").Select
Range("A56").Activate
ActiveWindow.SmallScroll Down:=-51
Range("I11:K16,I19:K24,I27:K34,H40:K44,H47:K51,A56:K64,C7:K7,I4:J4").Select
Range("I4").Activate
Selection.ClearContents
With Selection.Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.ClearContents
Range("C2").Value = "Select"
Range("C3").Value = "Select"
Range("C4").Value = "Select"
Range("C5").Value = "Select"
Range("I2").Value = "Select"
End Sub
0