As the name suggests, Max is used to finding the maximum value from a given data set or array. Although it is a worksheet function, one may use it with the worksheet method as a worksheet function. However, there is a limitation to this method as this function takes an array as an argument. Therefore, 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 and sum and find the minimum value and maximum value of the lot. To find the maximum value of the lot, we have an excel function called MAXThe MAX Formula in Excel is used to calculate the maximum value from a set of data/array. It counts numbers but ignores empty cells, text, the logical values TRUE and FALSE, and text values.read more, which will return the maximum value of the supplied range of numbers. In VBA, we do not have any built-in function called “MAX” to get the maximum number. We will see how to use this Excel VBA Max function.
Table of contents
- Excel VBA Max Function
- Example of Max Function in Excel VBA
- Advanced Example of Max in Excel VBA
- Things to Remember
- Recommended Articles
Example of Max Function in Excel VBA
Unfortunately, we do not 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 code below.
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
We have declared three variables to store the number in the above example.
Dim a As Integer Dim b As Integer Dim c As Integer
We have declared one more variable to show the results.
Dim Result As Integer.
For the first 3 three variables, we assigned values like 50, 25, and 60, respectively.
a = 50 b = 25 c = 60
In the next line, we have applied the MAX as a VBA worksheet functionThe worksheet function in VBA is used when we need to refer to a specific worksheet. When we create a module, the code runs in the currently active sheet of the workbook, but we can use the worksheet function to run the code in a particular worksheet.read more class to store the result to the variable “Result.”
Result = WorksheetFunction.Max(a, b, c)
So finally, we are showing the value in the message box in VBAVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided.read more.
MsgBox Result
We will run this code using F5 or manually and see the result in the message box.
So, the result is 60.
From all the supplied numbers: 50, 25, and 60, the maximum number is 60.
Advanced Example of Max in Excel VBA
Loops are crucial 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.
We have a list of items and the monthly sales performance of those items, as shown below.
Now for each item, we want to know the maximum sale number across four months, as shown in the picture.
By applying MAX to Excel, we can find this in a few seconds.
We will now see how to find the maximum value using the 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
It will identify the maximum number easily.
Run the code manually or press the F5 key to see the result below.
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
Based on the value provided by the VBA max function, the INDEX functionThe INDEX function in Excel helps extract the value of a cell, which is within a specified array (range) and, at the intersection of the stated row and column numbers.read more & MATCH functionThe MATCH function looks for a specific value and returns its relative position in a given range of cells. The output is the first position found for the given value. Being a lookup and reference function, it works for both an exact and approximate match. For example, if the range A11:A15 consists of the numbers 2, 9, 8, 14, 32, the formula “MATCH(8,A11:A15,0)” returns 3. This is because the number 8 is at the third position.
read more will return the associated month in the next line.
Things to Remember
- If their duplicate number is there, it will show only one number which comes first.
- It is the opposite formula of the MIN function in excelIn Excel, the MIN function is categorized as a statistical function. It finds and returns the minimum value from a given set of data/array.read more.
- The MAX function is not a VBA function. However, it is a built-in function in Excel, so use the worksheet function class.
You can download this Excel Template here – VBA Max Function Template.
Recommended Articles
This article has been a guide to VBA Max. Here, we learn how to use the Max function in VBA to find the maximum value from a supplied range of numbers, along with examples and downloadable codes. Below are some useful Excel articles related to VBA: –
- VBA FileCopy
- VBA Debug Print
- VBA FileSystemObject
- ByRef in VBA
- VBA Find and Replace
- Remove From My Forums
-
Question
-
in column A have a value related column D.
In effect in D are the value of Principal Agency and in column A are the
value of Sub Agency of Principal Agency.
Now based column D how to ,(with a function or macro) get and store in a
variable the value of min and max of Sub Agengy?Example:
for all 4580 in column D get the min and max from column A.
In this case the min is 4500 the max is 6552
The name of variable is MIN_Agency and MAX_Agency
Tks.Note: all column are in Text format
column A column D
0500 0580
0501 0580
0502 0580
0503 0580
0504 0580
0505 0580
0506 0580
0507 0580
0543 0580
0546 0580
0549 0580
0552 0580
0555 0580
0558 0580
0561 0580
0564 0580
0567 0580
0570 0580
0576 0580
0579 0580
0580 0580
0582 0580
1700 4780
1701 4780
1702 4780
1703 4780
1704 4780
1743 4780
1746 4780
1749 4780
1752 4780
1755 4780
1758 4780
2000 2380
2043 2380
2044 2380
2046 2380
2049 2380
2052 2380
2055 2380
2300 2380
2301 2380
2302 2380
2343 2380
2346 2380
2349 2380
2352 2380
2355 2380
2380 2380
2900 0580
2911 0580
2943 0580
2946 0580
2949 0580
3700 0580
3701 0580
3743 0580
3752 0580
3755 0580
3758 0580
4200 4780
4201 4780
4210 4780
4243 4780
4246 4780
4500 4580
4501 4580
4502 4580
4503 4580
4504 4580
4505 4580
4506 4580
4507 4580
4508 4580
4509 4580
4510 4580
4511 4580
4512 4580
4513 4580
4514 4580
4515 4580
4516 4580
4532 4580
4534 4580
4535 4580
4536 4580
4543 4580
4546 4580
4549 4580
4552 4580
4555 4580
4556 4580
4557 4580
4558 4580
4561 4580
4562 4580
4564 4580
4567 4580
4569 4580
4573 4580
4576 4580
4579 4580
4580 4580
4582 4580
4700 4780
4701 4780
4702 4780
4703 4780
4704 4780
4705 4780
4706 4780
4743 4780
4746 4780
4749 4780
4752 4780
4755 4780
4758 4780
4761 4780
4773 4780
4775 4780
4776 4780
4780 4780
5900 2380
5901 2380
5902 2380
5943 2380
5946 2380
6500 4580
6501 4580
6502 4580
6543 4580
6546 4580
6549 4580
6552 4580
6800 0580
6801 0580
6803 0580
6804 0580
6805 0580
6811 0580
6843 0580
6846 0580
Answers
-
Hi,
Note that this is a forum target at VSTO issues. There is also a VBA forum in this site.
However, I think, you can iterate through the Column D to finish that. You can refer all the cells in Column D using this statement: Me.Cells(4.i).Value (i is the row index). If this value is equal to 4580, then you get Me.Cells(1.i).Value. If this Value is greater than MAX_Agency, replace the Max_Agencey with it. If it is less than MIN_Agency, replace the MIN_Agency with it. After the iteration, you will get the max and min values in column A of 4580.
As to other values in Column D, the manipulate process is same.
Thanks
Ji
на сайте
http://msoffice.nm.ru/faq/macros/variables.htm#faq548
примеры посмотрел
Sub primer()
Dim iMassiv()
‘Здесь идёт заполнение массива некими данными
iMassiv = [a1:e10].Value
MsgBox Join(Application.Transpose(Application.Index(iMassiv, 0, 1)), vbNewLine), , «Первый столбец :»
‘или так
With Application
MsgBox Join(.Transpose(.Index(iMassiv, 0, 3)), _
Chr(10)), , «Третий столбец :»
End With
With WorksheetFunction
MsgBox Join(.Transpose(.Index(iMassiv, 0, 5)), _
Chr(13)), , «Пятый столбец :»
End With
Dim iAverage#, iMin#, iMax#, iSum#, iProduct
‘ For iCount = 1 To 100
‘ iMassiv(iCount) = Rnd * 1000
‘ Next
With Application
iAverage = .Average(iMassiv)
iMin = .Min(.Transpose(.Index(iMassiv, 0, 3)))
iMax = .Max(.Transpose(.Index(iMassiv, 0, 4)))
iSum = .Sum(iMassiv)
iProduct = .Product(iMassiv)
End With
End Sub
-
#2
Try posting a sample of your data and the required result.
-
#3
I don’t think you need VBA for this. Suppose your dates are in cells A1:A200, the fund names are in cells B1:B200, and the prices are in cells C1:C200. In cell D1, enter this formula:
=B1&C1
Copy this formula down through cell D200. Now, enter the fund name that you want to analyze in cell F1. Below that, in cell F2, enter the following array formula, and hit [Ctrl] + [Shift] + [Enter] (not just [Enter]):
=LARGE(($B$1:$B$200=$F$1)*($C$1:$C$200), 1)
If you used [Ctrl] + [Shift] + [Enter], you will see braces ({}) around the formula. This will find the largest price for the fund. Now, enter the following in cell F3:
=INDEX($A$1:$A$200, MATCH($F$1&$F$2, $D$1:$D$200, 0))
This will look up the first date that the highest price is hit for that fund.
Does that help?
-
#4
Code:
[FONT=Arial]Dim y As Integer<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT]
[FONT=Arial]Dim Maximum As String<o:p></o:p>[/FONT]
[FONT=Arial]Maximum = 0<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]For y = 1 To UBound(vtresult)<o:p></o:p>[/FONT]
[FONT=Arial] If vtresult(y, a, 0, 1) > Maximum Then<o:p></o:p>[/FONT]
[FONT=Arial] Maximum = vtresult(y, 0, 0, 1)<o:p></o:p>[/FONT]
[FONT=Arial] End If<o:p></o:p>[/FONT]
[FONT=Arial]Next<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]Range("K5") = Maximum[/FONT]
We have the above code, de a stands for the number of companies. We need to search the max for all companies and place this in a new array but we are stucked and don’t know how to procede!
-
#5
Basic algorithm for finding the max is like this:
Code:
Public Function FindMax(arr() As Variant, col As Long) As Long
Dim myMax As Long
Dim i As Long
For i = LBound(arr, 1) To UBound(arr, 1)
If arr(i, col) > myMax Then
myMax = arr(i, col)
FindMax = i
End If
Next i
End Function
FindMax(arr, 3) will return the index position of the largest number in column 3. You can then use that to return a corresponding value in a different column.
-
#6
Placing the max to an array seems to work now, but instead of stepping from a = a + 1 the process skips a = 1 and after de first loop it recognise a as 2 instead of 1. How is this possible and how can we fix this?
Rich (BB code):
Dim maxima() As Variant<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
ReDim maxima(company)<o:p></o:p>
<o:p></o:p>
Dim y As Integer<o:p></o:p>
Dim Maximum As String<o:p></o:p>
<o:p></o:p>
For a = 0 To company<o:p></o:p>
For y = 0 To UBound(vtresult)<o:p></o:p>
If vtresult(y, a, 0, 1) >= Maximum Then<o:p></o:p>
Maximum = vtresult(y, a, 0, 1)<o:p></o:p>
End If<o:p></o:p>
Next<o:p></o:p>
maxima(a) = Maximum<o:p></o:p>
a = a + 1<o:p></o:p>
Maximum = 0<o:p></o:p>
Next
-
#7
Basic algorithm for finding the max is like this:
Code:
Public Function FindMax(arr() As Variant, col As Long) As Long Dim myMax As Long Dim i As Long For i = LBound(arr, 1) To UBound(arr, 1) If arr(i, col) > myMax Then myMax = arr(i, col) FindMax = i End If Next i End Function
FindMax(arr, 3) will return the index position of the largest number in column 3. You can then use that to return a corresponding value in a different column.
Can you also give the respective code for finding the minimum? Just switching > to < doesn’t do the trick
Held would be much appreciated.
Many thanks!
-
#8
Remove the a= a + 1 The loop already is incrementing a. HTH. Dave
-
#9
Remove the a= a + 1 The loop already is incrementing a. HTH. Dave
But there is no a=a+1 in the maximization function given in my quote. Are you referrin to the question asked above mine?
-
#10
Yes. U need to start a new thread. Dave
R. Choate
-
#1
I’ve got an array that I load up using code. The array varies in size during resizing which takes place in an outer nested loop. My
problem is that I need to run through the array and find the largest integer that exists within it. This should be easy but I seem
to be having a brain fart. Lets say my array is called Prog(i), and I need to loop through it and find out which of the numbers is
the largest. For purposes of my question, lets just say that the array contains 5 elements, so I need to find out which of the 5
integers within the array is the largest.
Thanks in advance,
Advertisements
Norman Jones
-
#2
Hi R,
Try using the Excel Max function, e,g..
Sub Tester01()
Dim arr As Variant
arr = Array(100, 150, 75, 95, 22)
MsgBox Application.Max(arr)
End Sub
R. Choate
-
#3
This array is of variable size and contains variable contents. There is no easy situation like «Array(100, 150, 75, 95, 22)» used in
your example. My situation would be more like «Array(i)», where if i = 3, the array(i) would equal whatever is in that position
within the array. So I need the Max among all of the elements, represented by the variable i, as noted in my original post. For
instance, it would be more like array(1 to 50), where array(1) might be 62, array(15) might be 170, etc., etc.. Of course, Array(1
to i) is not proper syntax.
—
RMC,CPA
Hi R,
Try using the Excel Max function, e,g..
Sub Tester01()
Dim arr As Variant
arr = Array(100, 150, 75, 95, 22)
MsgBox Application.Max(arr)
End Sub
Alan Beban
-
#4
R. Choate said:
This array is of variable size and contains variable contents. There is no easy situation like «Array(100, 150, 75, 95, 22)» used in
your example. My situation would be more like «Array(i)», where if i = 3, the array(i) would equal whatever is in that position
within the array. So I need the Max among all of the elements, represented by the variable i, as noted in my original post. For
instance, it would be more like array(1 to 50), where array(1) might be 62, array(15) might be 170, etc., etc.. Of course, Array(1
to i) is not proper syntax.
If you want to return the *value* of the largest number in a 1-D array
named myArray, Norman Jones gave you the code:
Application.Max(myArray)
If you want to return the *array index number* of the largest number in
the array, then, you can use
Application.Match(Application.Max(myArray), myArray, 0) — 1 + LBound(arr)
Alan Beban
R. Choate
-
#5
No, Norman’s code does not work and the index number code indicated incorrect syntax in the editor. If I have an array where Prog(1)
= 15, Prog(2) = 0, and Prog(3) = 2, Norman’s code returns the «2» associated with Prog(3). I need for the code to return the «15»
after looping through all of them and determining that 15 was the largest element in the array. Here is the solution I finally
worked out on my own (Using option base 1):
High = 0
For A = 2 To BoxNums
If Prog(A) — 0 > High Then
High = Prog(A)
Else
GoTo goaroundagain
End If
goaroundagain:
Next
If High = 0 Then
Z = Prog(1) + High
ElseIf High < Prog(1) Then
Z = Prog(1)
Else
Z = High
End If
—
RMC,CPA
Alan Beban said:
This array is of variable size and contains variable contents. There is no easy situation like «Array(100, 150, 75, 95, 22)» used in
your example. My situation would be more like «Array(i)», where if i = 3, the array(i) would equal whatever is in that position
within the array. So I need the Max among all of the elements, represented by the variable i, as noted in my original post. For
instance, it would be more like array(1 to 50), where array(1) might be 62, array(15) might be 170, etc., etc.. Of course, Array(1
to i) is not proper syntax.
If you want to return the *value* of the largest number in a 1-D array
named myArray, Norman Jones gave you the code:
Application.Max(myArray)
If you want to return the *array index number* of the largest number in
the array, then, you can use
Application.Match(Application.Max(myArray), myArray, 0) — 1 + LBound(arr)
Alan Beban
Norman Jones
-
#6
Hi R,
No, Norman’s code does not work and the index number code indicated
incorrect syntax in the editor. If I have an array where Prog(1)
= 15, Prog(2) = 0, and Prog(3) = 2, Norman’s code returns the «2»
associated with Prog(3). I need for the code to return the «15»
Using your precise example, the following works for me, returning 15:
Sub Tester02()
Dim Prog(1 To 3) As Variant
Prog(1) = 15
Prog(2) = 0
Prog(3) = 2
MsgBox Application.Max(Prog)
End Sub
In my original response I used:
arr = Array(100, 150, 75, 95, 22)
simply as a convenient way to populate a 5 element 1-D array (as per your
example) and the sub was intended solely to demonstrate the use of the Excel
Max function to return the largest array element. This usage is independent
of the method by which the array is loaded. So however you load the array,
be that by assignment or by looping, a similar method should work.
BTW, in versions prior to xl2002, I believe that this will fail on arrays
with more than 5461 elements.
Advertisements
Dave Peterson
-
#7
Norman’s suggestion worked ok for me.
But Alan had a typo in his code: That last Arr should have been MyArray.
Option Explicit
Sub testme()
Dim Prog(1 To 3) As Long
Prog(1) = 15
Prog(2) = 0
Prog(3) = 2
Debug.Print «Max Value—> » & Application.Max(Prog)
Debug.Print «Index of Max Value—> » _
& Application.Match(Application.Max(Prog), Prog, 0) — 1 + LBound(Prog)
End Sub
Returned this in the immediate window:
Max Value—> 15
Index of Max Value—> 1
R. Choate
-
#8
I appreciate the attempts, but it did not work in my situation. Perhaps it had something to do with the dynamic resizing of the
arrays or something else that was in the more complicated actual code than what I gave in my example because I wanted to simplify
the specific problem and not sound confusing. In any case, I solved the issue so I won’t bother you guys any further on my post. It
is academic at this point.
Thanks again,
—
RMC,CPA
Norman’s suggestion worked ok for me.
But Alan had a typo in his code: That last Arr should have been MyArray.
Option Explicit
Sub testme()
Dim Prog(1 To 3) As Long
Prog(1) = 15
Prog(2) = 0
Prog(3) = 2
Debug.Print «Max Value—> » & Application.Max(Prog)
Debug.Print «Index of Max Value—> » _
& Application.Match(Application.Max(Prog), Prog, 0) — 1 + LBound(Prog)
End Sub
Returned this in the immediate window:
Max Value—> 15
Index of Max Value—> 1
Alan Beban
-
#9
Norman said:
Hi R,
Using your precise example, the following works for me, returning 15:
Sub Tester02()
Dim Prog(1 To 3) As Variant
Prog(1) = 15
Prog(2) = 0
Prog(3) = 2
MsgBox Application.Max(Prog)
End SubIn my original response I used:
arr = Array(100, 150, 75, 95, 22)
simply as a convenient way to populate a 5 element 1-D array (as per your
example) and the sub was intended solely to demonstrate the use of the Excel
Max function to return the largest array element. This usage is independent
of the method by which the array is loaded. So however you load the array,
be that by assignment or by looping, a similar method should work.BTW, in versions prior to xl2002, I believe that this will fail on arrays
with more than 5461 elements.
There are workarounds for this limitation with the functions from the
freely downloadable file at http://home.pacbell.net/beban
Alan Beban
Tom Ogilvy
-
#10
From an academic standpoint, there is nothing your code does that couldn’t
be done in one line with the Max function by replaceing *All* your code with
Z = Application.Max(prog)
Unless your array exceeds 5461 elements and you are using xl2000 or earlier.
Using your approach you have made it unnecessarily complex. If the max will
always be greater than zero then
Z = 0
for i = lbound(prog) to ubound(prog)
if prog(i) > Z then
Z = prog(i)
end if
Next
Would be the same.
—
Regards,
Tom Ogilvy
R. Choate said:
I appreciate the attempts, but it did not work in my situation. Perhaps it
had something to do with the dynamic resizing of the
arrays or something else that was in the more complicated actual code than
what I gave in my example because I wanted to simplify
the specific problem and not sound confusing. In any case, I solved the
issue so I won’t bother you guys any further on my post. It
is academic at this point.
Thanks again,
incorrect syntax in the editor. If I have an array where
R. Choate
-
#11
Perhaps something in the outer nesting of my code has prevented the simple solution from working. Believe me, I wanted it to work
and I tried it and I got the wrong answer. I know of you and I have great respect for your Excel knowledge. All I can say is that
there is something about my situation that is preventing the one line of code from working. Now that 2nd bit of code you gave me
below does work and is an improvement over what I wrote. However, remember that when I wrote it, I had been trying everything to get
the right answer. I went to the NG as a last resort, mostly because I do have experience with this and I should be able to write the
appropriate code for something so basic without going to the NG for an answer. Sometimes that approach leads to overly cumbersome
code as you have pointed out.
I do not have a large number of elements, either. Nowhere close to 5461. Again, I like that 2nd 6 lines of code you gave me and it
works, but the one line of code
Z = Application.Max(prog)
does not give me the right answer.
Hopefully I can save your patience and willingness to help me for another question later.
Thanks, Tom
Richard
—
RMC,CPA
From an academic standpoint, there is nothing your code does that couldn’t
be done in one line with the Max function by replaceing *All* your code with
Z = Application.Max(prog)
Unless your array exceeds 5461 elements and you are using xl2000 or earlier.
Using your approach you have made it unnecessarily complex. If the max will
always be greater than zero then
Z = 0
for i = lbound(prog) to ubound(prog)
if prog(i) > Z then
Z = prog(i)
end if
Next
Would be the same.
—
Regards,
Tom Ogilvy
R. Choate said:
I appreciate the attempts, but it did not work in my situation. Perhaps it
had something to do with the dynamic resizing of the
arrays or something else that was in the more complicated actual code than
what I gave in my example because I wanted to simplify
the specific problem and not sound confusing. In any case, I solved the
issue so I won’t bother you guys any further on my post. It
is academic at this point.
Thanks again,
incorrect syntax in the editor. If I have an array where
Advertisements
Postman
-
#12
Could you stick an example sheet somewhere so we can see it not working?
If Z = Application.Max(prog) isn’t working right it’s a worry.
P
| Perhaps something in the outer nesting of my code has prevented the simple
solution from working. Believe me, I wanted it to work
| and I tried it and I got the wrong answer. I know of you and I have great
respect for your Excel knowledge. All I can say is that
| there is something about my situation that is preventing the one line of
code from working. Now that 2nd bit of code you gave me
| below does work and is an improvement over what I wrote. However, remember
that when I wrote it, I had been trying everything to get
| the right answer. I went to the NG as a last resort, mostly because I do
have experience with this and I should be able to write the
| appropriate code for something so basic without going to the NG for an
answer. Sometimes that approach leads to overly cumbersome
| code as you have pointed out.
|
| I do not have a large number of elements, either. Nowhere close to 5461.
Again, I like that 2nd 6 lines of code you gave me and it
| works, but the one line of code
|
| Z = Application.Max(prog)
|
| does not give me the right answer.
|
| Hopefully I can save your patience and willingness to help me for another
question later.
|
| Thanks, Tom
|
| Richard
| —
| RMC,CPA
|
|
| From an academic standpoint, there is nothing your code does that couldn’t
| be done in one line with the Max function by replaceing *All* your code
with
|
| Z = Application.Max(prog)
|
| Unless your array exceeds 5461 elements and you are using xl2000 or
earlier.
|
|
| Using your approach you have made it unnecessarily complex. If the max
will
| always be greater than zero then
|
| Z = 0
| for i = lbound(prog) to ubound(prog)
| if prog(i) > Z then
| Z = prog(i)
| end if
| Next
|
| Would be the same.
|
| —
| Regards,
| Tom Ogilvy
|
|
| | > I appreciate the attempts, but it did not work in my situation. Perhaps
it
| had something to do with the dynamic resizing of the
| > arrays or something else that was in the more complicated actual code
than
| what I gave in my example because I wanted to simplify
| > the specific problem and not sound confusing. In any case, I solved the
| issue so I won’t bother you guys any further on my post. It
| > is academic at this point.
| >
| > Thanks again,
| > —
| > RMC,CPA
| >
| > «R. Choate» wrote:
| > >
| > > No, Norman’s code does not work and the index number code indicated
| incorrect syntax in the editor. If I have an array where
| > Prog(1)
| > > = 15, Prog(2) = 0, and Prog(3) = 2, Norman’s code returns the «2»
| associated with Prog(3). I need for the code to return the «15»
| > > after looping through all of them and determining that 15 was the
| largest element in the array. Here is the solution I finally
| > > worked out on my own (Using option base 1):
| > >
| > > High = 0
| > > For A = 2 To BoxNums
| > >
| > > If Prog(A) — 0| High Then
| > > High =
| Prog(A)
| > > Else
| > > GoTo
| goaroundagain
| > > End If
| > >
| > > goaroundagain:
| > >
| > > Next
| > > If High = 0
Then
| > > Z =
Prog(1)
| + High
| > > ElseIf High <
| Prog(1) Then
| > > Z =
Prog(1)
| > > Else
| > > Z = High
| > > End If
| > > —
| > > RMC,CPA
| > >
|
|
|
R. Choate
-
#13
I’m sure that the function is working fine. I’ve obviously got something in my outer nesting layers which led to the failure of the
function in my application. I have a pretty good sized procedure and I know that if I post it, then I would need to explain some
things and then I would be subject to all kinds of suggestions and/or criticisms regarding my code. Since I’m still working
feverishly on this project and need to get it done, I don’t think I’ll post the code this time. I do appreciate everybody for taking
an interest in this little snippet of my project. I hope I will be so lucky again when I have a better challenge. As a matter of
fact, I think I’ve got a good one now, which I’ll post seperately in just a few minutes.
Thanks again!
Richard
—
RMC,CPA
Could you stick an example sheet somewhere so we can see it not working?
If Z = Application.Max(prog) isn’t working right it’s a worry.
P
| Perhaps something in the outer nesting of my code has prevented the simple
solution from working. Believe me, I wanted it to work
| and I tried it and I got the wrong answer. I know of you and I have great
respect for your Excel knowledge. All I can say is that
| there is something about my situation that is preventing the one line of
code from working. Now that 2nd bit of code you gave me
| below does work and is an improvement over what I wrote. However, remember
that when I wrote it, I had been trying everything to get
| the right answer. I went to the NG as a last resort, mostly because I do
have experience with this and I should be able to write the
| appropriate code for something so basic without going to the NG for an
answer. Sometimes that approach leads to overly cumbersome
| code as you have pointed out.
|
| I do not have a large number of elements, either. Nowhere close to 5461.
Again, I like that 2nd 6 lines of code you gave me and it
| works, but the one line of code
|
| Z = Application.Max(prog)
|
| does not give me the right answer.
|
| Hopefully I can save your patience and willingness to help me for another
question later.
|
| Thanks, Tom
|
| Richard
| —
| RMC,CPA
|
|
| From an academic standpoint, there is nothing your code does that couldn’t
| be done in one line with the Max function by replaceing *All* your code
with
|
| Z = Application.Max(prog)
|
| Unless your array exceeds 5461 elements and you are using xl2000 or
earlier.
|
|
| Using your approach you have made it unnecessarily complex. If the max
will
| always be greater than zero then
|
| Z = 0
| for i = lbound(prog) to ubound(prog)
| if prog(i) > Z then
| Z = prog(i)
| end if
| Next
|
| Would be the same.
|
| —
| Regards,
| Tom Ogilvy
|
|
| | > I appreciate the attempts, but it did not work in my situation. Perhaps
it
| had something to do with the dynamic resizing of the
| > arrays or something else that was in the more complicated actual code
than
| what I gave in my example because I wanted to simplify
| > the specific problem and not sound confusing. In any case, I solved the
| issue so I won’t bother you guys any further on my post. It
| > is academic at this point.
| >
| > Thanks again,
| > —
| > RMC,CPA
| >
| > «R. Choate» wrote:
| > >
| > > No, Norman’s code does not work and the index number code indicated
| incorrect syntax in the editor. If I have an array where
| > Prog(1)
| > > = 15, Prog(2) = 0, and Prog(3) = 2, Norman’s code returns the «2»
| associated with Prog(3). I need for the code to return the «15»
| > > after looping through all of them and determining that 15 was the
| largest element in the array. Here is the solution I finally
| > > worked out on my own (Using option base 1):
| > >
| > > High = 0
| > > For A = 2 To BoxNums
| > >
| > > If Prog(A) — 0| High Then
| > > High =
| Prog(A)
| > > Else
| > > GoTo
| goaroundagain
| > > End If
| > >
| > > goaroundagain:
| > >
| > > Next
| > > If High = 0
Then
| > > Z =
Prog(1)
| + High
| > > ElseIf High <
| Prog(1) Then
| > > Z =
Prog(1)
| > > Else
| > > Z = High
| > > End If
| > > —
| > > RMC,CPA
| > >
|
|
|
Cool Sport
-
#14
Is there any good way to deal with a very big array (like 1000 rows and
150 columns).
Tom Ogilvy
-
#15
Dim varr as Variant
varr = Range(«A1»).Resize(1000,150).Value
for i = lbound(varr,1) to ubound(varr,1)
for j = lbound(varr,2) to ubound(varr,2)
‘ process array element varr( i , j )
next j
Next i
Range(«A1002»).Resize(1000,150).Value = varr
Alan Beban
-
#16
I don’t know what you mean by «a good way», but if the functions in the
freely downloadable file at http://home.pacbell.net/beban are available
to your workbook, and given that myArray is a 1000×150 array:
maxVal = ArrayMax1D(MakeArray(myArray, 1))
Alan Beban
Advertisements
R. Choate
-
#17
I don’t know if I should feel bad for posting a problem that created a monster, or feel good for bringing up an issue that generated
lots of interest. I only wish I could get so much response to my «Intersection» post. Nobody wants to touch that one at all.
—
RMC,CPA
Is there any good way to deal with a very big array (like 1000 rows and
150 columns).
Cool Sport
-
#18
Sorry for interrupting the thread.
By the way, I have some suggestions. As I know that Excel Max function
is not the fastest way to utilize in vba code.
Let say you have an array named Prog(size)
The following sub will do the job you want. I assume that you have an
array of integers.
Option Base 1
Sub getMax(tArray as Variant) As Integer
Dim size As Integer, i As Integer
Dim maxVal As Integer
maxVal = 0 ‘ defalt return value
size = UBound(tArray)
If size>0 then
maxVal = tArray(1)
If size>2 then
For i = 2 to size
If tArray(i)>maxVal then maxVal = tArray(i)
Next i
End If
End If
getMax = maxVal
End Sub
There is another way is that you can sort the array acsendingly then get
the last element which is the largest one. I have a sub to do this task
that can be posted if you like.
Hope you find this useful.
R. Choate
-
#19
I’m a little bit rusty on my declaration rules, but I can see that you had a typo mixing the declaration of the array with the sub
name. However, when I tried the declaration as I thought you meant it, I got the red syntax error text. Perhaps you would take
another look at the code you posted. I’m hoping to keep it for later reference. I know I will need to do this again sometime.
Thanks,
Richard
—
RMC,CPA
Sorry for interrupting the thread.
By the way, I have some suggestions. As I know that Excel Max function
is not the fastest way to utilize in vba code.
Let say you have an array named Prog(size)
The following sub will do the job you want. I assume that you have an
array of integers.
Option Base 1
Sub getMax(tArray as Variant) As Integer
Dim size As Integer, i As Integer
Dim maxVal As Integer
maxVal = 0 ‘ defalt return value
size = UBound(tArray)
If size>0 then
maxVal = tArray(1)
If size>2 then
For i = 2 to size
If tArray(i)>maxVal then maxVal = tArray(i)
Next i
End If
End If
getMax = maxVal
End Sub
There is another way is that you can sort the array acsendingly then get
the last element which is the largest one. I have a sub to do this task
that can be posted if you like.
Hope you find this useful.
Advertisements
Cool Sport
-
#20
lol, sorry for my silly mistake. I wrote the code straight away from my
mind without checking it. It is supposed to be a FUNCTION that returns
value instead of procedure.
Please replace Sub with Function at the first and last statement
Function ….
…..
End Function
If your array declaration is like this:
Dim prog(5) As Integer
Then the statement below should work:
Msgbox getMax(prog)
It should work (I have tested it).
Sorry again,