+ Reply to Thread
Results 1 to 9 of 9

Max function for all columns

Hybrid View

  1. #1
    Registered User
    Join Date
    10-19-2016
    Location
    New York City, New York
    MS-Off Ver
    2015
    Posts
    13

    Max function for all columns

    Hi I'm trying to find the max value for each column with this code, but it stops after a blank row. I want the code to find the max of the entire column even if there are blank rows in the data. Any idea how to fix this code? Or is there a better way to write the code? Thanks!

    'Outputting max value for each column
    Sub Max_Columns()
    
        With Range("A2").CurrentRegion
            Range("A1").Resize(, .Columns.Count).FormulaR1C1 = "=MAX(R[2]C:R[" & .Rows.Count & "]C)"
        End With
        
        'Paste values
        Rows(1).EntireRow.Copy
        Rows(1).PasteSpecial xlPasteValues
    
    End Sub
    Last edited by alansidman; 10-21-2016 at 12:51 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,035

    Re: Max function for all columns

    Code Tags Added
    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 at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    10-19-2016
    Location
    New York City, New York
    MS-Off Ver
    2015
    Posts
    13

    Re: Max function for all columns

    Sorry! Thank you - I'll remember to do this for my next posts.

  4. #4
    Registered User
    Join Date
    10-19-2016
    Location
    New York City, New York
    MS-Off Ver
    2015
    Posts
    13

    Re: Max function for all columns

    Hi Alan, should I re-post this in order to try to get a response?

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,671

    Re: Max function for all columns

    If you are trying to replace the values in each cell of row one with the max of column then try:
    Option Explicit
    
    Sub ColMaxes()
        Dim Headers As Range, _
            TestCol As Range, _
            LastRow As Long
    
        Set Headers = Range("A1", Cells(1, Columns.Count).End(xlToLeft))
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        
        For Each TestCol In Headers
            TestCol = WorksheetFunction.Max(TestCol.Resize(rowsize:=LastRow))
        Next TestCol
        
    End Sub
    EDIT: Does each column have the same number of rows?
    Last edited by protonLeah; 10-23-2016 at 04:59 PM.
    Ben Van Johnson

  6. #6
    Registered User
    Join Date
    10-19-2016
    Location
    New York City, New York
    MS-Off Ver
    2015
    Posts
    13

    Re: Max function for all columns

    Hi protonLeah, no the columns may not have the same number of rows. Also, I'm not trying to replace the values in the first row. The first row could be blank, but I still want it to output the max value for every column that has data. Does this make sense?

    Thank you!

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,671

    Re: Max function for all columns

    Your original code puts a formula in row 1 then copies and paste special back to row 1? (tested). So where should the max be written?

  8. #8
    Registered User
    Join Date
    10-19-2016
    Location
    New York City, New York
    MS-Off Ver
    2015
    Posts
    13

    Re: Max function for all columns

    Sorry for the confusion! I just wrote code that works for what I need

     Sub Max_Columns1()
    
        With Range("A2").CurrentRegion
            Range("A1").Resize(, .Columns.Count).FormulaR1C1 = "=MAX(R[2]C:R[" & Cells(Rows.Count, "A").End(xlUp).Row & "]C)"
        End With
        
        'Paste values
        Rows(1).EntireRow.Copy
        Rows(1).PasteSpecial xlPasteValues
    
    End Sub

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    24,035

    Re: Max function for all columns

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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: 2
    Last Post: 03-05-2013, 05:36 PM
  2. Replies: 5
    Last Post: 10-26-2012, 02:21 PM
  3. Replies: 3
    Last Post: 10-27-2010, 03:11 AM
  4. Function recognizes two columns and adds total of two other columns
    By grafx77 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-18-2007, 09:09 AM
  5. Using a function to compare three columns
    By brainfreeze in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-19-2006, 06:38 AM
  6. Need Function to Compare two columns
    By mikess314 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-11-2006, 06:37 PM
  7. [SOLVED] min function from different columns
    By sonofroy in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-06-2005, 06:06 PM
  8. [SOLVED] 'IF' FUNCTION for 2 columns
    By Lisa in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-26-2005, 01:06 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