+ Reply to Thread
Results 1 to 6 of 6

VBA numerical range/limit.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2018
    Location
    gloucester
    MS-Off Ver
    2016
    Posts
    17

    VBA numerical range/limit.

    I'm trying to create VBA for a simple grading system. User enters score in text box...if they input letters message box asking for numbers only appears,
    would like also for them only to input numbers between 0 and 100 with message box if they put higher or lower values in. I cant get the later working. wondering if its as simple as my code being wrong?

    Private Sub cmdCalculate_Click()
    Dim Num1 As Single
    Dim Num2 As Single
    Dim Num3 As Single
    Dim Answer As Single
    
    Num1 = Val(txtEnglish.Text)
    Num2 = Val(txtMaths.Text)
    Num3 = Val(txtScience)
    
    Answer = (Num1 + Num2 + Num3) / 3
    TxtAnswer.Text = Str(Answer)
    
    If Not (IsNumeric(txtEnglish.Text & txtMaths.Text & txtScience.Text)) Then
        MsgBox ("Please enter numeric scores only")
        txtEnglish.Text = ""
        txtMaths.Text = ""
        txtScience.Text = ""
    If Not (IsNumeric(txtEnglish.Value & txtMaths.Value & txtScience.Value >0 & <100 txtEnglish.Text & txtMaths.Text & txtScience.text)) Then
        MsgBox ("Scores between 0 and 100 only")
        txtEnglish.Text = ""
        txtMaths.Text = ""
        txtScience.Text = ""
    
    End If
    
     
    If TxtAnswer.Text >= 50 Then lblPassFail.Caption = "PASS"
    If TxtAnswer.Text <= 49 Then lblPassFail.Caption = "FAIL"
    
    End Sub
    Last edited by suedavies123; 03-01-2019 at 03:18 PM. Reason: tags

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA numerical range/limit.

    Wouldn't it be easier to put the grades in cells with data validation (allow decimal numbers between 0 and 100) and use simple formulas to average and determine pass/fail?

    Please edit your post to add CODE tags. The forum rules explain how.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    12-17-2018
    Location
    gloucester
    MS-Off Ver
    2016
    Posts
    17

    Re: VBA numerical range/limit.

    apologies thought I had added tags. it might be easier but I have got this far and this has bought me to a halt. if there is no way of doing it then i will go back to the drawing board...

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA numerical range/limit.

    Thanks for the code tags.

    Private Sub cmdCalculate_Click()
      Dim ctl           As Variant
      Dim dVal          As Double
      Dim dAvg          As Double
    
      For Each ctl In Array(txtEnglish, txtMaths, txtScience)
        If IsNumeric(ctl.Text) Then
          dVal = CDbl(ctl.Text)
          If dVal < 0 Or dVal > 100 Then
            MsgBox ctl.Name & ": Grades must be 0..100!"
            ctl.Value = ""
            Exit Sub
          End If
    
        Else
          MsgBox ctl.Name & ": Value must be numeric!"
          ctl.Value = ""
          Exit Sub
        End If
    
        dAvg = dAvg + dVal
      Next ctl
    
      dAvg = dAvg / 3
      
      txtAnswer.Text = Format(dAvg, "0.0")
      If dAvg >= 50 Then lblPassFail.Caption = "PASS" Else lblPassFail.Caption = "FAIL"
    End Sub

  5. #5
    Registered User
    Join Date
    12-17-2018
    Location
    gloucester
    MS-Off Ver
    2016
    Posts
    17

    Re: VBA numerical range/limit.

    WOW...you are one clever person! Thank you! It works a dream.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA numerical range/limit.

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Extraction of numerical digits from a mixed alpha numerical Excel cell.
    By JustWilliam in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-05-2016, 01:44 PM
  2. Add All Values in Range, Whether Numerical or Not
    By Esuric in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 07-16-2014, 04:17 PM
  3. [SOLVED] IF Statement with Numerical Range
    By Rhiannon25 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-30-2013, 09:49 PM
  4. [SOLVED] Help with replacing a numerical range with one value
    By Lechoz in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-25-2013, 12:49 AM
  5. Avoiding non-numerical values in a range
    By wwsd in forum Excel General
    Replies: 5
    Last Post: 01-01-2009, 05:50 PM
  6. [SOLVED] Range with numerical data
    By Moises in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2005, 07:06 AM
  7. [SOLVED] Determing a range from numerical data
    By Moises in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2005, 02:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1