+ Reply to Thread
Results 1 to 6 of 6

Code required for Offset, to find max values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-16-2013
    Location
    World
    MS-Off Ver
    Excel 2007
    Posts
    215

    Question Code required for Offset, to find max values

    Dear all experts,

    Need a code,Kindly find the sample output excel file for offset and max value finder task.

    Thanking you all,

    Best regards.
    Attached Files Attached Files
    Last edited by pvsvprasad; 08-18-2016 at 12:37 PM.

  2. #2
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Code required for Offset, to find max values

    Though it can be easily done with the help of the formulas, try the below code to see if that is what you are trying to achieve...

    Sub GetMaxValueAndHeader()
    Dim lr As Long, lc As Long, i As Long, j As Long
    Dim rng As Range
    Dim mVal As Double
    Dim Header As String
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 3 To lr
       For j = 1 To lc
          If Abs(Cells(i, j)) > mVal Then
             mVal = Abs(Cells(i, j))
             Header = Cells(2, j)
          End If
       Next j
       Cells(i, 5) = Header
       Cells(i, 6) = mVal
       Header = ""
       mVal = 0
    Next i
    End Sub
    If you need a formula based solution, try these Array Formulas which require confirmation with Ctrl+Shift+Enter instead of Enter alone.

    In F3
    =MAX(ABS(A3:D3))
    In F3
    =INDEX($A$2:$D$2,MATCH(H3,ABS(A3:D3),0))
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  3. #3
    Forum Contributor
    Join Date
    12-16-2013
    Location
    World
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: Code required for Offset, to find max values

    Quote Originally Posted by sktneer View Post
    Though it can be easily done with the help of the formulas, try the below code to see if that is what you are trying to achieve...

    Sub GetMaxValueAndHeader()
    Dim lr As Long, lc As Long, i As Long, j As Long
    Dim rng As Range
    Dim mVal As Double
    Dim Header As String
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 3 To lr
       For j = 1 To lc
          If Abs(Cells(i, j)) > mVal Then
             mVal = Abs(Cells(i, j))
             Header = Cells(2, j)
          End If
       Next j
       Cells(i, 5) = Header
       Cells(i, 6) = mVal
       Header = ""
       mVal = 0
    Next i
    End Sub
    If you need a formula based solution, try these Array Formulas which require confirmation with Ctrl+Shift+Enter instead of Enter alone.

    In F3
    =MAX(ABS(A3:D3))
    In F3
    =INDEX($A$2:$D$2,MATCH(H3,ABS(A3:D3),0))
    Dear sir,
    thank you for your kind reply.

    your code is good but negative value is not printed.

    for example, your code is produced 6.10 instead of -6.10 @ F3 cell

    and produced 6.50 instead of -6.50 @ F5 cell.

    kindly fix this bug.

    thanking you sir.

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Code required for Offset, to find max values

    Don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    For normal conversational replies, try using the QUICK REPLY box below.

    Please try this....


    Sub GetMaxValueAndHeader()
    Dim lr As Long, lc As Long, i As Long, j As Long
    Dim rng As Range
    Dim vVal As Double,mVal As Double
    Dim Header As String
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    lc = 4
    For i = 3 To lr
       For j = 1 To lc
          If Abs(Cells(i, j)) > vVal Then
             vVal = Abs(Cells(i, j))
             mVal = Cells(i, j)
             Header = Cells(2, j)
          End If
       Next j
       Cells(i, 5) = Header
       Cells(i, 6) = mVal
       Header = ""
       vVal = 0
       mVal = 0
    Next i
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-16-2013
    Location
    World
    MS-Off Ver
    Excel 2007
    Posts
    215

    Re: Code required for Offset, to find max values

    sorry for late reply.

    your code is good.

    thanking you sir.

  6. #6
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Code required for Offset, to find max values

    You're welcome. Glad to help.

    If that takes care of your original question, please mark your thread as Solved by selecting Thread Tools (just above your first post) --> Mark thread as solved.

+ 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: 03-29-2015, 11:59 AM
  2. [SOLVED] To find a value in a row then to offset values based on code. *tricky*
    By ExcelNewblet in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-27-2015, 12:25 PM
  3. Copy and PasteSpecial Values after Find and Offset
    By SteveG1965 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2013, 06:35 AM
  4. Replies: 1
    Last Post: 04-19-2013, 08:30 PM
  5. Help with find, copy, and paste code using offset
    By mundellj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2011, 08:16 PM
  6. Code To Find and Offset works on first sheet but won't loop to other sheets
    By mgaworecki in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-10-2009, 03:02 PM
  7. code required to find similar entries in two columns
    By Duguid1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2008, 09:52 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