Solved

Excel VB Max & Min value + cell address

Posted on 2013-01-22
12
1,216 Views
Last Modified: 2013-01-24
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))

         Sheet4.Cells(15, lCol).Value = Sheet4.MaxAddress(ThisRange).Value
         Sheet4.cells(15, lCol -2) = cell.address.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
   
        If cel = Application.WorksheetFunction.Max(ThisRange) Then MaxAddress = cel.Address
   
    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
Comment
Question by:mike637
  • 6
  • 4
  • 2
12 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38808237
Hi, mike637.

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

Regards,
Brian.
0
 

Author Comment

by:mike637
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

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

The one actually causing your error...
Sheet1.MaxAddress(ThisRange).Value
...should be...
Sheet1.MaxAddress(ThisRange)

Finally...
Cell.Address.Offset(, -2)
...is that an undefined variable?

Regards,
Brian.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:mike637
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

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

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

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

Thanks,
Brian.
0
 
LVL 26

Assisted Solution

by:redmondb
redmondb earned 250 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

by:mike637
ID: 38808291
As requested,

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

Michael
MonthyBusinessReview.xlsm
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
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).Value = MaxAddress(ThisRange)
         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
   
        If cel = Application.WorksheetFunction.Max(ThisRange) Then MaxAddress = cel.Address
   
    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

Open in new window

0
 
LVL 26

Expert Comment

by:redmondb
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

by:
Saqib Husain, Syed earned 250 total points
ID: 38808530
I have written my own routine called getmaxmin2 in the module1. Check it out
Copy-of-MonthyBusinessReview-3.xlsm
0
 

Author Closing Comment

by:mike637
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

by:redmondb
ID: 38814775
Thanks, Michael.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question