[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Case WorksheetFunction problem

Posted on 2014-08-06
10
Medium Priority
?
262 Views
Last Modified: 2014-08-06
Folks,
In the WorksheetChange event I am using the following code to verify a correct answer for a function, Mode.Sngl:
Private Sub Worksheet_Change(ByVal Target As Range)
 Application.EnableEvents = False
 On Error GoTo ErrorRoutine
 Select Case True
 Case Range("P7").Value = ""
 'The user hasn't given an answer so just clear any messaage that might be in P7
    Range("P7").Value = ""
    'We know at this point that there's something in P7 so se if it is the right answer
 Case WorksheetFunction.ModeSngl(Range("N6:N15")) = Range("P7").Value
 'Correct answer but see if he used a formula
 If InStr(1, Range("P7").Formula, "MODE.SNGL", vbTextCompare) > 0 Then
    Range("Q7").Value = "Correct"
 Else
    Range("Q7").Value = "Correct but please use the MODE.SNGL function"
 End If
 Case WorksheetFunction.ModeSngl(Range("N6:N15")) <> Range("P7").Value
    'Incorrect answer
    Range("Q7").Value = "Incorrect"
    Case Else
        Range("Q7").Value = "Incorrect"
  End Select
  Application.EnableEvents = True
  Exit Sub
ErrorRoutine:
 Range("Q7").Value = Range("Q7").Text & " Error - read Remarks"
 Application.EnableEvents = True
End Sub

Open in new window

The problem I am having is at line 9:
Case WorksheetFunction.ModeSngl(Range("N6:N15")) = Range("P7").Value
The function is Mode.Sngl, however, when I enter that instead of ModeSngl I get the following compile error:
mode-error.jpgI do not know of a solution since I will have other functions similar to the one above.
0
Comment
Question by:Frank Freese
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 40

Assisted Solution

by:als315
als315 earned 100 total points
ID: 40244420
Screenshot shows mistype:
WorksheetFunction.Mode.Sngl
instead of
WorksheetFunction.ModeSngl
0
 

Author Comment

by:Frank Freese
ID: 40244422
OK...I found the error. It should be Mode_Sngl. Great! However, I'm not getting the results from line 11 as being "Correct"
0
 
LVL 10

Assisted Solution

by:FamousMortimer
FamousMortimer earned 400 total points
ID: 40244437
Hi,
You can use WorksheetFunction.Mode_Sngl(Range("N6:N15"))

Just keep in mind that it will throw an Application Error if the range does not have a mode average so you will have to handle it if that is a possibility
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Assisted Solution

by:FamousMortimer
FamousMortimer earned 400 total points
ID: 40244453
To help debug and find the cause add this line before Case WorksheetFunction.ModeSngl(Range("N6:N15")) = Range("P7").Value

MsgBox "Is " & WorksheetFunction.Mode_Sngl(Range("N6:N15")) & " equal to " & Range("P7").Value & "?"

Open in new window

0
 

Author Comment

by:Frank Freese
ID: 40244700
OK...this is on my laptop and I'm creating a monthly image now. Will get back later
0
 
LVL 50

Accepted Solution

by:
Martin Liss earned 1500 total points
ID: 40245187
Here is corrected code.

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
 On Error GoTo ErrorRoutine
 Select Case True
    Case Range("P7").Value = ""
    'The user hasn't given an answer so just clear any messaage that might be in P7
        Range("P7").Value = ""
       'We know at this point that there's something in P7 so se if it is the right answer
    Case WorksheetFunction.Mode_Sngl(Range("N6:N15")) = Range("P7").Value
        'Correct answer but see if he used a formula
        If InStr(1, Range("P7").Formula, "MODE.SNGL", vbTextCompare) > 0 Then
           Range("Q7").Value = "Correct"
        Else
           Range("Q7").Value = "Correct but please use the MODE.SNGL function"
        End If
    Case WorksheetFunction.Mode_Sngl(Range("N6:N15")) <> Range("P7").Value
       'Incorrect answer
       Range("Q7").Value = "Incorrect"
       Case Else
           Range("Q7").Value = "Incorrect"
  End Select
  Application.EnableEvents = True
  Exit Sub
ErrorRoutine:
 Range("Q7").Value = Range("Q7").Text & " Error - read Remarks"
 Application.EnableEvents = True
End Sub

Open in new window

0
 

Author Closing Comment

by:Frank Freese
ID: 40245202
thanks folks
appreciate the code change Martin
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 40245210
You're welcome.
0
 

Author Comment

by:Frank Freese
ID: 40245217
I've been chipping away at those Statistical worksheets where there was a single result, such as AVERAGE. That just leaves figuring out how to evaluate those worksheets with multiple results, such as AVERAGEA or PERCENTILE but that's another question.
BTW, no feedback on the Consolidated project, at all.
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 40245228
No news is good news.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

830 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