To get the maximum value in Excel VBA, use Application.WorksheetFunction.Max on a range, column or list of numbers, or loop through cells with a variable if you need to skip blanks, errors or hidden rows. The one-line version is MsgBox Application.WorksheetFunction.Max(Range("A1:A10")), which returns the largest number in A1:A10 and behaves exactly like the MAX function on the sheet. Everything below explains how to place that line in a working macro, how to handle the cases where it fails, and how to get the result into a cell instead of a message box.

What You'll Need

  • Excel on Windows or Mac with macros enabled (the Developer tab visible, or the ability to open the VBA editor with Alt+F11 on Windows, or the Developer tab on Mac).
  • A workbook saved as .xlsm, since a standard .xlsx file discards macro code when closed.
  • Some numeric data in a range, for example a column of sales figures, so you can test the code.
  • Macro security set to allow macros in trusted locations, or the file placed in a trusted folder.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows, or on Mac use Tools > Macro > Visual Basic Editor from the Excel menu. Menu labels vary slightly between Excel versions and between Windows and Mac.
  2. Insert a module. In the editor choose Insert > Module. Code placed in a module is available to the whole workbook; code placed in a sheet's own code window only runs on that sheet's events.
  3. Write the basic macro. Type the following, replacing the range with your own:
    Sub ShowMax()
        Dim result As Double
        result = Application.WorksheetFunction.Max(Range("A1:A10"))
        MsgBox "The maximum is " & result
    End Sub
    This mirrors the worksheet MAX function and returns the largest numeric value in the range.
  4. Run it. Press F5 with the cursor inside the procedure, or go back to Excel and use Developer > Macros, select ShowMax and click Run. A message box should show the largest number.
  5. Write the answer to a cell instead. Message boxes are fine for testing, but most people want the result stored. Replace the MsgBox line with Range("C1").Value = result. Now the maximum appears in C1 each time the macro runs.
  6. Use a named or dynamic range. Instead of hard-coding A1:A10, point at a whole column with Range("A:A"), or use Range("A1").End(xlDown) to find the last filled cell in a list. A whole-column reference is safe with WorksheetFunction.Max because it ignores text and blanks.
  7. Handle an empty range before calling Max. If every cell in the range is blank or text, WorksheetFunction.Max raises a run-time error 1004. Guard it:
    If Application.WorksheetFunction.Count(Range("A1:A10")) > 0 Then
        result = Application.WorksheetFunction.Max(Range("A1:A10"))
    Else
        result = 0
    End If
    Count only counts numbers, so it tells you whether Max has anything to work with.
  8. Get the position of the maximum, not just the value. Use Match against the Max result:
    Dim maxVal As Double
    Dim pos As Variant
    maxVal = Application.WorksheetFunction.Max(Range("A1:A10"))
    pos = Application.Match(maxVal, Range("A1:A10"), 0)
    This returns the relative row number of the first cell holding that value.
  9. Find the maximum across several ranges or an array. WorksheetFunction.Max accepts multiple arguments: Application.WorksheetFunction.Max(Range("A1:A10"), Range("C1:C10"), 5) returns the largest of all of them. You can also pass a VBA array of numbers directly.
  10. Loop when you need conditions. WorksheetFunction.Max cannot ignore hidden rows or apply criteria. For that, loop through the cells and compare:
    Dim c As Range, best As Double
    best = -1E+300
    For Each c In Range("A1:A100").Cells
        If IsNumeric(c.Value) And Not IsEmpty(c.Value) Then
            If c.EntireRow.Hidden = False And c.Value > best Then best = c.Value
        End If
    Next c
    Start from a very small number so negative values are still captured.
  11. Save as .xlsm and test on a copy. Save the workbook in the macro-enabled format, then run the macro on a duplicate of your data before using it on the real file.

Common Mistakes

  • Using Max on a range containing only text or blanks. This throws error 1004, not zero. Guard with Count as shown above.
  • Forgetting the Application prefix. Writing WorksheetFunction.Max without Application. usually still works inside Excel, but the fully qualified form is clearer and avoids confusion when the code is reused.
  • Assuming Max ignores errors. If the range contains #N/A or #DIV/0!, WorksheetFunction.Max returns that error rather than the largest number. Clean the data or loop with error checks.
  • Starting a loop from zero. If all your values are negative, initialising the best value to 0 means the macro reports 0. Start from a very large negative number instead.
  • Hard-coding ranges. A macro tied to A1:A10 breaks the moment rows are added. Use whole columns or a last-row calculation.
  • Saving as .xlsx. The macro is silently dropped on close and the file reopens with no code.

Tips

  • Use Evaluate or [MAX(A1:A10)] as a short alternative when you just need a value, though WorksheetFunction is easier to read and debug.
  • Store the result in a Double, not an Integer, so large or decimal values are not truncated.
  • If the maximum should update automatically, put the MAX formula in a cell and read that cell from VBA instead of recalculating in code.
  • Use Application.WorksheetFunction.MaxIfs for a single condition (available in modern Excel versions) and a loop for anything more complex.
  • Name your ranges in Excel (Formulas > Define Name) and refer to them in code by name, which survives inserted rows.

Troubleshooting

Run-time error 1004. The range is empty, contains only text, or the sheet reference is wrong. Check the sheet name is spelled exactly and that numbers are stored as numbers, not text with leading apostrophes.

The macro returns 0 unexpectedly. Either the range really contains a zero, or your loop started from zero and all values are negative. Check the starting value.

The macro returns an error value like #N/A. An error cell is inside the range. Use Application.WorksheetFunction.Max only after removing errors, or switch to a loop that skips cells where IsError(c.Value) is true.

Nothing happens when you run it. Macros may be disabled. Check the security banner at the top of the window and enable content for this file, or move the file to a trusted location.

The result is stale. If you read a formula cell, make sure calculation is automatic (Formulas > Calculation Options > Automatic) or call Application.Calculate before reading.

The code works on one sheet and not another. Unqualified Range calls act on the active sheet. Qualify with Worksheets("Data").Range(...) to be explicit.

Frequently Asked Questions

Does WorksheetFunction.Max include hidden rows?

Yes. It behaves like the worksheet MAX function and includes hidden and filtered-out rows. Loop through visible cells only if you need to exclude them.

What is the difference between Max and MaxA?

Max ignores logical values and text in ranges; MaxA counts TRUE as 1 and text as 0. Use Max for numeric data.

Can I get the maximum of a filtered list?

Not directly with WorksheetFunction.Max. Use the SUBTOTAL function with function number 104 in a cell, or loop over SpecialCells(xlCellTypeVisible).

Why does Max return a date serial number?

Dates are stored as numbers. Format the destination cell as a date to display it correctly.

Can I use Max on a whole column?

Yes, Range("A:A") works and is fast, but it still errors if the column has no numbers at all.

Does this work on Excel for Mac?

Yes, WorksheetFunction.Max is available in VBA on Mac. Menu locations differ, but the code is the same.