Consulting

Results 1 to 4 of 4

Thread: HELP FIND HIGHEST VALUE USING VBA

  1. #1

    HELP FIND HIGHEST VALUE USING VBA

    For my assignment, I have to create a macro that will find the highest value in a range along with the corresponding date. I created a macro but it only evaluates the first value in the range. How do I fix this?
    Sub findhighestprice()
    Dim maxprice As Currency
    Dim maxdate As Date
    Dim i As Integer
    For i = 1 To 252
    maxprice = Range("B2").Value
    maxdate = Range("A2").Value
    If Range("A2").Offset(i, 0) > Range("B2").Offset(i, 1) Then
        maxprice = Range("B2").Offset(i, 1)
        maxdate = Range("A2").Offset(i, 0)
    End If
    Next i
    Range("s5").Value = maxprice
    Range("s6").Value = maxdate
    End Sub
    Last edited by Lolo122298; 11-05-2017 at 08:18 PM. Reason: Added code tags

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    In your macro there is no need for
    maxprice = Range("B2").Value 
            maxdate = Range("A2").Value
    As they do nothing, your argument that you have put in asks the question IF the DATE of any offset row from A2 to A254 is greater than any value (Currency)in the 2nd column then....?
    How do you propose to evaluate an integer against a DATE?
    Secondly, if you do happen to find an argument that is true you want maxprice to be the value of Range "C2" + I e.g "C176"

    You need to look at using Application.WorksheetFunction and using the MAX function, you can make use of FIND to get you the address then display the OFFSET to give you the DATE.

    As it is an assignment I cannot give you the solution but will try to help coach you to it
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Thank you so much!!! I finally got it!

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    It looks more an Excel question than a VBA question:

    Sub M_snb()
       MsgBox [index(A1:A252,match(max(B1:B252),B1:B252,0))]
    End Sub

Posting Permissions

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