+ Reply to Thread
Results 1 to 4 of 4

Macro to Identify Largest Value Across Columns, Then Subtract Value Held in Other Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    Jacksonville, Florida
    MS-Off Ver
    Excel 2010
    Posts
    16

    Macro to Identify Largest Value Across Columns, Then Subtract Value Held in Other Cell

    I need a macro to identify the largest value across columns I2 - AS2. Once found, I need to replace the value with value found minus the value in BQ. The final step would be to highlight the cell that was changed across columns I2 - AS2, then move to the next row and do same until I reach the last row in the worksheet. The number of rows vary each day but the columns do not.

    I have written the attached code, which identifies the largest value in the first row, but I don't know how to replace the value found with the new value and then move to the next row until it reaches the final row for the day.

    In the attached file, my largest value sits in AC2. That value needs to be replaced with 486.17 (1,175.37 - the value in BQ2 of 689.20). The cell should then be highlighted so I can quickly identified what cells have been changed.

    Any help would be appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Macro to Identify Largest Value Across Columns, Then Subtract Value Held in Other Cell

    give this a try

    Sub Largest_Value_Highlight_Address()
    'Determines largest values from the active worksheet
    
        Dim dataSet As String
        Dim rng As Range
        Dim sValue As Variant
        Dim Col As Range
        Dim iRow As Long
        Dim sAddress As Range
        Dim LastRow As Integer, x As Integer
    
        LastRow = Cells(999999, 1).End(xlUp).Row
    
        For x = 2 To LastRow
            dataSet = "I" & x & ":AS" & x
            Set rng = Range(dataSet)
            sValue = Application.WorksheetFunction.Max(rng)
            For Each Col In rng.Columns
                If Application.WorksheetFunction.CountIf(Col, sValue) > 0 Then
                    iRow = Application.WorksheetFunction.Match(sValue, Col, 0)
                    Set sAddress = Col.Cells(iRow, 1)
                    sAddress.Select
                    Cells(x, sAddress.Cells.Column).Value = Cells(x, sAddress.Cells.Column).Value - Cells(x, 69).Value
                    With Selection
                        .Interior.Color = RGB(255, 255, 0)
                    End With
                End If
            Next Col
        Next x
    
    End Sub
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,596

    Re: Macro to Identify Largest Value Across Columns, Then Subtract Value Held in Other Cell

    Late entry ... ?

    Option Explicit
    
    Sub TMS()
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    Dim rFound As Range
    Dim i As Long
    
    Dim lLR As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To lLR
        On Error Resume Next
        Set rFound = Range("I" & i).Offset(0, awf.Match(awf.Max(Range("I" & i & ":AS" & i)), Range("I" & i & ":AS" & i), 0) - 1)
        With rFound
            ' prevent code running twice
            If .Interior.Color <> RGB(255, 255, 0) Then
                .Value = .Value - Range("BQ" & i).Value
                .Interior.Color = RGB(255, 255, 0)
            End If
        End With
        On Error GoTo 0
    Next 'i
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,596

    Re: Macro to Identify Largest Value Across Columns, Then Subtract Value Held in Other Cell

    Thanks for the rep

+ 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. Identified Largest and Smallest VALUES, but need to Identify Location
    By wilburr in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-07-2013, 12:51 AM
  2. How to use and step through cell references held as text in a sheet in a macro
    By runswick in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-22-2013, 01:21 PM
  3. How to write a macro to sort many columns from largest to smallest
    By brainiack18 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2011, 03:51 PM
  4. Formlula: Subtract Largest Number then Average
    By alan_stephen75@ in forum Excel General
    Replies: 5
    Last Post: 10-10-2009, 12:13 PM
  5. Replies: 1
    Last Post: 01-26-2007, 02:25 PM

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