Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I found that the following code is getting max value in range:
Cells(Count, 4)=Application.WorksheetFunction.Max(Range(Cells(m, 1),Cells(n, 1)))
How can I search within a specific sheet? Data
sheet in this case
Like:
Worksheets(d).Cells(x, 4).Value = Worksheets("Data")... ????? FIND MAX ????
This will often work, but is missing a reference:
worksheets("Data").Cells(Count, 4)= Application.WorksheetFunction.Max _
( worksheets("Data").range( cells(m,1) ,cells(n,1) )
The text "cells" should be preceded by a reference to which worksheet the cells are on, I would write this:
worksheets("Data").Cells(Count, 4) = Application.WorksheetFunction.Max _
( worksheets("Data").range( worksheets("Data").cells(m,1) ,worksheets("Data").cells(n,1) )
This can also be written like this which is clearer:
with worksheets("Data")
.Cells(Count, 4) = Application.WorksheetFunction.Max _
( .range( .cells(m,1) ,.cells(n,1) )
End With
I hope this helps.
Harvey
–
–
–
You can pass any valid excel cell reference to the range method as a string.
Application.WorksheetFunction.Max(range("Data!A1:A7"))
In your case though, use it like this, defining the two cells at the edges of your range:
Application.WorksheetFunction.Max _
(range(worksheets("Data").cells(m,1),worksheets("Data").cells(n,1)))
–
–
–
–
–
If you want to process quickly milion of cells to find MAX/MIN, you need heavier machinery. This code is faster then Application.WorksheetFunction.Max
.
Function Max(ParamArray values() As Variant) As Variant
Dim maxValue, Value As Variant
maxValue = values(0)
For Each Value In values
If Value > maxValue Then maxValue = Value
Max = maxValue
End Function
Function Min(ParamArray values() As Variant) As Variant
Dim minValue, Value As Variant
minValue = values(0)
For Each Value In values
If Value < minValue Then minValue = Value
Min = minValue
End Function
Stolen from here: https://www.mrexcel.com/forum/excel-questions/132404-max-min-vba.html
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.