+ Reply to Thread
Results 1 to 2 of 2

How to find the second highest value by VBA. Always give me 0 as the answer

  1. #1
    Registered User
    Join Date
    01-08-2016
    Location
    Toronto
    MS-Off Ver
    MS Office 2010
    Posts
    0

    Angry How to find the second highest value by VBA. Always give me 0 as the answer

    Private Sub CommandButton1_Click()
    Dim rng As Range, cell As Range
    Dim highestvalue As Double, secondhighestvalue As Double

    Set rng = Selection
    highestvalue = 0
    secondhighestvalue = 0


    'find the highest value
    For Each cell In rng
    If cell.VALUE > highestvalue Then highestvalue = cell.VALUE
    Next cell

    'find the second highest value
    For Each cell In rng
    If cell.VALUE > secondhighestvalue And cell.VALUE < highestvalue Then
    secondhighestvalue = cell.VALUE

    highestvalue = highestvalue + 1
    secondhighestvalue = secondhighestvalue + 1

    End If
    Next cell

    MsgBox "second highest value is" & secondhighestvalue
    End Sub

  2. #2
    Registered User
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,164
    Hello qiuxue2003,

    Welcome to Excelforum. Be a part of large Excel community. Enjoy Learning.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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. Replies: 4
    Last Post: 08-28-2015, 08:43 AM
  2. How to get excel SOLVER to give out more than one answer??
    By KPee44 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2015, 04:02 PM
  3. Comparing data to give an answer?
    By Bogus93 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-27-2014, 11:47 AM
  4. Count, add, discriminate and give me an answer
    By LawrieAJ in forum Excel General
    Replies: 4
    Last Post: 07-13-2013, 08:24 AM
  5. [SOLVED] I need a current formula to only give me an answer if column W says DUI Help please! TY
    By LaurieLaurie in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-12-2013, 08:30 PM
  6. [SOLVED] Need help with formula to give 0 if value is negative or answer is
    By Sharona77 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-01-2006, 04:45 PM
  7. [SOLVED] Is there a function to give us the highest value in a list of tex
    By Calaw in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-11-2005, 06:55 AM

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