+ Reply to Thread
Results 1 to 10 of 10

Find negative numbers in a table and return the value and corresponding text

  1. #1
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Find negative numbers in a table and return the value and corresponding text

    Hi

    Hopefully somebody can guide me in the right direction..

    I have a table with 2 columns:

    Column A always contains text
    Column B always contains numbers
    The table does not have a fixed amount of rows and will be continually increasing over time

    I want a macro that will search the table from top to bottom for the first set of values with a negative number in column B, then return that set of values (Column A & Column B) to any 2 cells in the spreadsheet (lets say A1 and B1)

    So for instance using the attached example the macro will find the values 'Text 2' and '-0.1' and put them into the cells A1 & B1.

    If no negative values are found then I want to simply exit the macro.

    Thanks in advance!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Find negative numbers in a table and return the value and corresponding text

    You can do this with formulas, if you're willing. Try the formula below in A1. It must be array-entered (confirmed with Ctrl + Shift + Enter instead of Enter):

    =IFERROR(INDEX(A$4:A$12,MIN(IF($B$4:$B$12<0,ROW($B$4:$B$12)-ROW($B$4)+1))),"No Negatives")

    Fill right into B1. It should update automatically as your data changes. If you have more rows than you've included in your sample, just change all of the "12"s in the formula to match or exceed what you have.
    Attached Files Attached Files
    If your problem has been solved, please use "Thread Tools" to mark the thread as "Solved".

    If you're grateful for this site's existence and would like to contribute to the community, please consider posting something hilarious in our joke thread:
    https://www.excelforum.com/the-water...ke-thread.html

  3. #3
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Find negative numbers in a table and return the value and corresponding text

    Sorry I should have made it clear but the reason I need to use VBA is because I have another macro that will copy the negative values to another table then delete them.

    I want to combine the 2 together so I can automate the function, as currently the user has to manually enter the values into A1 & B1.

  4. #4
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: Find negative numbers in a table and return the value and corresponding text

    @ormerods, from your description, I conclude that the sheet formulas will be the best.
    This is an array formula so needs to be entered using Ctrl-Shift-Enter.
    A1 formula, drag to the right right
    Please Login or Register  to view this content.
    Best Regards,
    Maras.

  5. #5
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Find negative numbers in a table and return the value and corresponding text

    If the formulas return the correct results, and you just want them incorporated into VBA, you could use the following, which contains two options (put formulas in cells, or values):

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Find negative numbers in a table and return the value and corresponding text

    Thanks that should work perfectly, I hadn't heard of the Evaluate function before that should definitely come in handy in future.

  7. #7
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Find negative numbers in a table and return the value and corresponding text

    I just tried to implement this into my sheet and I noticed that the the IFERROR function doesn't seem to be working (unless I'm doing something wrong?)

    Even if all the values in the B column are positive it still returns the top value, not the "No Negatives" string it should do.

    Am I missing something?

  8. #8
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Find negative numbers in a table and return the value and corresponding text

    Good catch, sorry - I should have paid more attention when adding my IFERROR clause. The following should toss out a pop-up notification if there are no negatives:


    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    10-27-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Find negative numbers in a table and return the value and corresponding text

    Perfect, thanks!

  10. #10
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Find negative numbers in a table and return the value and corresponding text

    My pleasure, good luck!

+ 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: 3
    Last Post: 01-12-2017, 03:54 PM
  2. Replies: 4
    Last Post: 01-18-2015, 04:52 PM
  3. Replies: 3
    Last Post: 09-15-2014, 07:56 PM
  4. Find lowest value from 4 numbers in various cells and return text
    By mheinmiller in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-27-2014, 05:32 AM
  5. [SOLVED] Having two date return number but exclude negative numbers
    By redjayhawk11 in forum Excel General
    Replies: 5
    Last Post: 12-31-2013, 06:34 AM
  6. Return zero for negative numbers.
    By pdmkh in forum Excel General
    Replies: 1
    Last Post: 01-20-2011, 07:53 AM
  7. Find, return string address in a range of cells with numbers and text
    By Vera22 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-09-2007, 08:37 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