+ Reply to Thread
Results 1 to 6 of 6

Find Highest Value Based on Condition

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Find Highest Value Based on Condition

    Hi all

    In my file I have two numbers, can one category
    New lines are added as needed by the users.

    Col. A contains the "sector number" with column B indicating the "sub sector number".
    Col. B always uses the value in col A as prefix and is ascending in number
    Col. C shows whether the product is "NORM" or "SPEC" - in case of "SPEC" the number does follow it's own numbering logic.

    What I would like to achieve is that the user can, via input box, enter the "sector number" and the macro should return the next available number in a message box under "NORM" conditions - please see attached file

    Sample.xlsx

    How would I code such conditions, bearing in mind that my list is several 1000 lines long

    Thanks
    FD
    Last edited by FallingDown; 01-06-2015 at 11:04 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Find Highest Value Based on Condition

    Hi FallingDown,
    try this
    Sub ertert()
    Dim sn&, x, i&
    sn = Application.InputBox("Enter the sector number", , 510, Type:=1)
    If sn = 0 Then Exit Sub
    
    x = Range("A1:C" & Cells(Rows.Count, 1).End(xlUp).Row).Value
    For i = UBound(x) To 2 Step -1
        If x(i, 1) = sn Then
            If x(i, 3) = "NORM" Then
                Cells(i, 4) = "Result: " & x(i, 2) + 1: Exit For
            End If
        End If
    Next i
    
    If i = 1 Then Cells(Rows.Count, 1).End(xlUp)(2, 1).Resize(, 4).Value = _
            Array(sn, sn * 1000 + 1, "NORM", "Result: " & sn * 1000 + 1)
    End Sub

  3. #3
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Find Highest Value Based on Condition

    HI nilem

    It's not quite what I am looking for

    However, I got something in the meanwhile

    
    Sub NextNum()
    Dim LR As Long, cell As Range, rng As Range
    s = InputBox("Enter Sector")
    
    
        For Each cell In Range("a2:c30")
            If cell.Value = s Then
            cell.Rows.Select
                If rng Is Nothing Then
                    Set rng = cell
                Else
                    If InStr(cell.Offset(0, 2).Value, "NORM") Then Set rng = Union(rng, cell)
                End If
            End If
        Next cell
        
        rng.EntireRow.Select
        MsgBox (Application.WorksheetFunction.Max(rng.EntireRow)) + 1
    
    
    
    End Sub
    I did not specify in my original request, and in the sample as well, that apart from NORM there are other Categories as well
    the problem is that my code only looks at value with NORM. What it should do is IGNORE values containing *SPEC*.

    can I use the InStr function to NOT include the value specified?

  4. #4
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Find Highest Value Based on Condition

    Hi everyone and happy new year

    Anybody an idesa how I can use instr (or something else) to not include a certain string?

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Find Highest Value Based on Condition

    Hi FallingDown,
    try to replace this
    If InStr(cell.Offset(0, 2).Value, "NORM") Then Set rng = Union(rng, cell)
    with this
    If InStr(cell.Offset(0, 2).Value, "SPEC") = 0 Then Set rng = Union(rng, cell)

  6. #6
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Find Highest Value Based on Condition

    Thanks, that did it!

+ 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: 1
    Last Post: 01-10-2014, 01:55 PM
  2. VBA code to find highest and 2nd highest number based in criteria
    By Michael007 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-25-2011, 08:38 AM
  3. Find highest based on criteria
    By timtrag12 in forum Excel General
    Replies: 2
    Last Post: 03-27-2011, 05:42 PM
  4. Replies: 2
    Last Post: 02-23-2011, 09:45 AM
  5. Replies: 2
    Last Post: 10-26-2009, 06:43 AM

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