Finding out max value from used range

xsdip

New Member
Joined
May 21, 2019
Messages
28
Hi,

Can anyone please give me some inputs.

I have to find out the maximum value of all the entered values in the excel sheet.

The no of entries in each rows differs. For eg : A column may contain 30 rows. B column contains 40 rows etc.

I used this code. But it takes lot of time. Is the for each loop makes the issue? I am using used range option in for each loop.


For each a in activesheet.usedrange
If b<abs(a) then
b = abs(a)
End if
Next a

When i run this code, I am getting type mismatch error also because since the rows are different in each column.

I think selecting the used cells alone will clear the problem. Can anyone please help on this.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
VBA Code:
MaxValue = Application.Max(ActiveSheet.UsedRange)
This should do the job

However... it will return an error if the usedrange includes an error condition.

To skip potential errors we need to move to a more complex solution:
VBA Code:
Dim wArr, MaxValue
'
wArr = ActiveSheet.UsedRange.Value
MaxValue = Application.Max(wArr)
If IsError(MaxValue) Then
    For I = 1 To UBound(wArr)
        For j = 1 To UBound(wArr, 2)
            If IsError(wArr(I, j)) Then
                wArr(I, j) = "Z"
            End If
        Next j
    Next I
    MaxValue = Application.Max(wArr)
End If
As you see, we try to get the result from the used range, but if it fails then we scan the array with copy of the used range and remove the error conditions

Bye
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,805
Members
449,127
Latest member
Cyko

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top