Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Excel VB Max & Min value + cell address

Posted on 2013-01-22
Medium Priority
1,387 Views
Hello Experts,

I have hit a wall in trying to get data calculated correctly in my routine.  I keep getting [Invalid Qualifer].

I want to find the max range in a column then find the address of that cell and use the offset for another cell.

Here is my code as it stands now that does not work:

Sub FindMaxMin() 'this is a sheet routine

Dim ThisRange As Range
Dim lRow As Long
Dim lCol As Long

For lRow = 3 To 13
For lCol = 3 To 3     '(I cut this to 3 To 3 for testing, but it will be 3 TO 24)
'r = 15 To 19

ThisRange = (Range(lRow, lCol))

'Next r  - I pulled this out and defaulted to row 15 just for testing.

Next lCol

Next lRow

End Sub

Function MaxAddress(ByRef ThisRange As Range) As String

Dim cel As Range

For Each cel In ThisRange

Next cel

' I need to add these functions after I get the first one to work properly.

'FirstMax = Application.WorksheetFunction.Max(rng) - this is row 15
'NextMax = Application.WorksheetFunction.Large(rng, 2)  - this is row 16
'FirstMin = Application.WorksheetFunction.Min(rng)  - this is row 17
'NextMin = Application.WorksheetFunction.Small(rng, 2) - this is row 18

End Function

Thanks,
Michael
0
Question by:mike637
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4
• 2

LVL 26

Expert Comment

ID: 38808237
Hi, mike637.

This line...
ThisRange = (Range(lRow, lCol))
...should be...
Set ThisRange = (Range(lRow, lCol))

Regards,
Brian.
0

Author Comment

ID: 38808243
Hi Brian,

This still errored out as bad qualifier.

Would it be easier if I sent you the workbook on this?  I really need some help or guidance.

Michael
0

LVL 26

Expert Comment

ID: 38808249
Apologies, mike637. I was too quick. There are two further items...

The one actually causing your error...
...should be...

Finally...
...is that an undefined variable?

Regards,
Brian.
0

Author Comment

ID: 38808262
Brian,

The Cell.Address offset is a variable but it is always related to the address of the MaxAddress.

Michael
0

LVL 26

Expert Comment

ID: 38808278
<blush> And...
Set ThisRange = (Range(lRow, lCol))
...should be...
Set ThisRange = (cells(lRow, lCol))

I an issue with...
...but there's another problem - if Cell is defined (and set) as a range, you'r eprobably looking for...

So, maybe posting the file would be a good idea!

Thanks,
Brian.
0

LVL 26

Assisted Solution

redmondb earned 1000 total points
ID: 38808282
mike637,

Apologies, crossing posts, so please see my previous post.

The Cell.Address offset is a variable but it is always related to the address of the MaxAddress.
From what you've posted, it's neither a defined nor a set variable. Even if its definition is elsewhere it's not connected to MaxAddress.

Regards,
Brian.
0

Author Comment

ID: 38808291
As requested,

Just tell me where to send the check to - so you receive it.

Michael
0

LVL 43

Expert Comment

ID: 38808292
Try
``````Sub FindMaxMin() 'this is a sheet routine

Dim ThisRange As Range
Dim lRow As Long
Dim lCol As Long

For lRow = 3 To 13
For lCol = 3 To 3     '(I cut this to 3 To 3 for testing, but it will be 3 TO 24)
'r = 15 To 19

Set ThisRange = (Cells(lRow, lCol))

Sheet4.Cells(15, lCol - 2) = Range(MaxAddress(ThisRange)).Offset(, -2)

'Next r  - I pulled this out and defaulted to row 15 just for testing.

Next lCol

Next lRow

End Sub

Function MaxAddress(ByRef ThisRange As Range) As String

Dim cel As Range

For Each cel In ThisRange

Next cel

' I need to add these functions after I get the first one to work properly.

'FirstMax = Application.WorksheetFunction.Max(rng) - this is row 15
'NextMax = Application.WorksheetFunction.Large(rng, 2)  - this is row 16
'FirstMin = Application.WorksheetFunction.Min(rng)  - this is row 17
'NextMin = Application.WorksheetFunction.Small(rng, 2) - this is row 18

End Function
``````
0

LVL 26

Expert Comment

ID: 38808304
mike637,

Two of us working on this separately is only going to cause confusion, so I'll step aside as long as ssaqibh's on it.

Regards,
Brian.
0

LVL 43

Accepted Solution

Saqib Husain, Syed earned 1000 total points
ID: 38808530
I have written my own routine called getmaxmin2 in the module1. Check it out
0

Author Closing Comment

ID: 38814657
Thank you Brian and ssaquibh,

Your guidance has helped out a lot.  I split the points on this to be fair to both - expecially when both of you were taking out your time to assist.

Thanks again,
Michael
0

LVL 26

Expert Comment

ID: 38814775
Thanks, Michael.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,â€¦
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to usâ€¦
###### Suggested Courses
Course of the Month8 days, 21 hours left to enroll