+ Reply to Thread
Results 1 to 6 of 6

Sub for Lowest Negative

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2016
    Location
    NY
    MS-Off Ver
    Excel 2013
    Posts
    3

    Sub for Lowest Negative

    Hello,

    I am trying to write a procedure in VBA for excel to find the lowest negative value from a selected range. If there are no negative values, then a message box should display "There are no negative values" Below is what I have so far, but I'm not sure if it si correct or how to compare each case to the previous. Thanks for any help.


    Sub LowestNegative()
    
    Dim Lowest As Single
    
    For Each c In Selection.Cells
    Select Case c.Value
        Case Is < 0
        Lowest = ActiveCell.Value
        Case Is > 0
        Lowest = MsgBox("There are no negative values.")
        Case Else
        Lowest = MsgBox("There are no negative values.")
    End Select
    Next
    
    
    End Sub
    Last edited by axm3688; 05-17-2016 at 01:05 PM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,053

    Re: Sub for Lowest Negative

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    05-17-2016
    Location
    NY
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Sub for Lowest Negative

    Hello,

    I am trying to write a procedure in VBA for excel to find the lowest negative value from a selected range. If there are no negative values, then a message box should display "There are no negative values" Below is what I have so far, but I'm not sure if it si correct or how to compare each case to the previous. Thanks for any help.


    Sub LowestNegative()
    
    Dim Lowest As Single
    
    For Each c In Selection.Cells
    Select Case c.Value
        Case Is < 0
        Lowest = ActiveCell.Value
        Case Is > 0
        Lowest = MsgBox("There are no negative values.")
        Case Else
        Lowest = MsgBox("There are no negative values.")
    End Select
    Next
    
    
    End Sub

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,420

    Re: Sub for Lowest Negative

    Before exploring your code, why not simply use the =MIN() function in a spreadsheet cell, or accessed in VBA (see help file here, https://msdn.microsoft.com/en-us/lib.../ff197608.aspx with an example that uses the MIN() function)?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    05-17-2016
    Location
    NY
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Sub for Lowest Negative

    Thank you, I've updated my code to the following

    Sub LowestNegative()
    
    Dim Lowest As Single
    Dim MyRange As Range
    
    Set MyRange = Selection.Cells
    Lowest = Application.WorksheetFunction.Min(MyRange)
    If Lowest < 0 Then
        MsgBox ("The lowest negative value is" + Str(Lowest))
    ElseIf Lowest > 0 Then
        MsgBox ("There are no negative values")
    ElseIf Lowest = 0 Then
        MsgBox ("There are no negative values")
    End If
    End Sub
    However, as a follow up, is there a way i can gte it to find and highlight the cell with the value instead of displaying it in a MsgBox like I have it now? Thanks again.

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,420

    Re: Sub for Lowest Negative

    I tend to use the .Match() function for this. See the other example in the help file.

    The Match() function will tell you what row the lowest number is in, then you can use the .select or .activate method to activate a cell in that row, or you can use some other means (change font or background color) to highlight one or more cells in that row. This help file describes the .interior property and shows how to use it to change the cell color: https://msdn.microsoft.com/en-us/lib.../ff836210.aspx

+ 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. Lowest Day in Lowest Month
    By jeck876 in forum Excel General
    Replies: 4
    Last Post: 12-16-2015, 07:25 PM
  2. [SOLVED] how to find the lowest to highest value and display the name who is the lowest and the hig
    By jasond1992 in forum Excel Programming / VBA / Macros
    Replies: 25
    Last Post: 09-23-2015, 09:20 PM
  3. Recording lowest and 2nd lowest numbers in a rang of cells
    By ORIELSON in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-01-2015, 10:31 AM
  4. [SOLVED] Lowest price & Lowest lead time
    By thup_98 in forum Excel - New Users/Basics
    Replies: 11
    Last Post: 01-22-2013, 03:21 PM
  5. Excluding negative numbers and bringing back lowest postive number
    By Dancingqueen in forum Excel - New Users/Basics
    Replies: 6
    Last Post: 09-15-2009, 03:40 PM
  6. Find lowest number in groups then lowest overall.
    By swieduwi in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-24-2008, 01:00 PM
  7. Range vs. lowest #, 2nd lowest #, 3rd lowest #, etc
    By jwebb in forum Excel General
    Replies: 2
    Last Post: 03-09-2005, 09:06 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