# VBA Max Article byJeevan A Y Max, as the name suggests, is used to finding out the maximum value from a given data set or an array, this is a worksheet function so it is used with the worksheet method as worksheet function, there is a limitation to this method as this function takes an array as an argument there can only be 30 values in the array.

## Excel VBA Max Function

We have several numerical functions in excel. We can count numerical values in the range, we can sum, and we can find the minimum value as well the maximum value of the lot. To find the maximum value of the lot, we have an , which will return the maximum value of the supplied range of numbers. In VBA, we don’t have any built-in function called “MAX” to get the maximum number. We will see how to use this Excel VBA Max Function.

For eg:
Source: VBA Max (wallstreetmojo.com)

### Example of Max Function in Excel VBA

Unfortunately, we don’t have the luxury of using MAX as the VBA built-in function, but we can access this function as a part of the Worksheet Function class.

Now, look at the below code.

Code:

```Sub MAX_Example1()

Dim a As Integer
Dim b As Integer
Dim c As Integer

Dim Result As Integer

a = 50
b = 25
c = 60

Result = WorksheetFunction.Max(a, b, c)

MsgBox Result

End Sub```

In the above example, I have declared three variables to store the number.

```Dim a As Integer

Dim b As Integer

Dim c As Integer```

I have declared one more variable to show the results.

`Dim Result As Integer.`

For the first 3 three variables, I have assigned the value like 50, 25, and 60, respectively.

```a = 50

b = 25

c = 60```

In the next line, I have applied the MAX as a class to store the result to the variable “Result.”

`Result = WorksheetFunction.Max(a, b, c)`

Now I will run this code using F5 or manually and see what the result is in the message box.

So, the result is 60.

From all the supplied numbers, i.e., 50, 25, and 60, the maximum number is 60.

### Advanced Example of Max in Excel VBA

Loops are very important in VBA to run through all the cells and arrive at the result. We will see how to combine VBA MAX with loops to arrive at the maximum value from the list of numbers.

I have a list of items and their monthly sales performance of those items, as shown below.

Now for each item, I want to know what the maximum sale number is across 4 months, as shown in the picture.

By applying MAX to excel, we can find this in a few seconds.

Now we will see how to find the maximum value by using VBA code.

The below code will perform the task of finding the maximum number for each item.

Code:

```Sub MAX_Example2()

Dim k As Integer

For k = 2 To 9
Cells(k, 7).Value = WorksheetFunction.Max(Range("A" & k & ":" & "E" & k))
Next k

End Sub```

This will identify the maximum number easily.

Now run the code manually or press the F5 key and see the result as shown below.

In order to get the maximum values month name, use the below code.

Code:

```Sub MAX_Example2()

Dim k As Integer

For k = 2 To 9
Cells(k, 7).Value = WorksheetFunction.Max(Range("B" & k & ":" & "E" & k))
Cells(k, 8).Value = WorksheetFunction.Index(Range("B1:E1"), WorksheetFunction.Match _
(Cells(k, 7).Value, Range("B" & k & ":" & "E" & k)))
Next k

End Sub```

### Things to Remember

• If their duplicate number is there, it will show only one number which comes first.
• This is the opposite formula of the .
• MAX is not a VBA function. It is a built-in function in excel, so using the worksheet function class.

You can download this Excel Template here – VBA Max Function Template.

### Recommended Articles

This has been a guide to VBA Max. Here we learn how to use Max Function in VBA to find the maximum value from a supplied range of numbers along with examples and downloadable codesBelow are some useful excel articles related to VBA –

• 3 Courses
• 12 Hands-on Projects
• 43+ Hours
{"ft_lang_label":"__label__en","ft_lang_prob":0.82349634,"math_prob":0.97654146,"size":5785,"snap":"2021-43-2021-49","text_gpt3_token_len":1334,"char_repetition_ratio":0.15948798,"word_repetition_ratio":0.07156488,"special_character_ratio":0.23111495,"punctuation_ratio":0.10602205,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99884814,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-22T19:08:49Z\",\"WARC-Record-ID\":\"<urn:uuid:a96f5f27-b859-425d-8203-443e97fa496e>\",\"Content-Length\":\"197183\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2b3a13ad-f59c-4e3e-9378-ebfb66f5336e>\",\"WARC-Concurrent-To\":\"<urn:uuid:6e53de09-a5ed-44ff-9414-3fee26ec01d3>\",\"WARC-IP-Address\":\"34.211.154.233\",\"WARC-Target-URI\":\"https://www.wallstreetmojo.com/vba-max/\",\"WARC-Payload-Digest\":\"sha1:HGHQQLY2B7AOJALJ2NM6RRNIWGCECXA4\",\"WARC-Block-Digest\":\"sha1:HMLYQYDPOV5NQKRACNBK4OLU3FQNOISG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585518.54_warc_CC-MAIN-20211022181017-20211022211017-00516.warc.gz\"}"}