Maxif with critera help

Kellogg

New Member
Joined
Mar 30, 2013
Messages
41
Platform
  1. Windows
  2. MacOS
Dear Great Minds of Excel,

I'm having an issue with the conversion from a cell formula to VBA code. I'm using a User Form to enter data and would like the maximum number that matches a particular identifier. I developed the array formula and it works well. But I'm having a problem converting to vba code attached to a button.

Scenario: I have multiple columns.
Column A equals the entry number.
Column C equals the vehicle Tag number.
Column H equals the total miles.


A C (Tag) H (miles) P
1 A12345 100 A12345
2 A11122 250
3 A12345 175
4 A11222 200
5 A12345 500
6 A11122 300

What I want to do is enter the Tag number and find the maximum miles when I press a button on my user form. So, I enter A12345 into a text field, press a button and get 500. The code below works when in a cell and P1 is the tag number.

{=MAX(IF($C$1:$C$9=$P$1,$H$1:$H$9))} ' This works

My issue is when I translate to VBA code

I have the following:

Code:
Dim VehicleTagRange As Range
Dim MaxMilesRange As Range
Dim VehTag
Dim lnglast As Long
Dim Maxif As Variant
Dim TagNmbr
'
'
    lnglast = Range("A" & Rows.Count).End(xlUp).Row
    TagNmbr = "A12345"
'
'
Set VehicleTagRange = Range("C1:C" & lnglast)
Set MaxMilesRange = Range("H1:H" & lnglast)
Set VehTag = "A12345"
'
'
Maxif = Application.Evaluate("=Max(IF(" & VehicleTagRange.Address & "=" & TagNmbr & "," & MaxMilesRange.Address & "))")
'
MsgBox Maxif       'This returns 0
'
'
'
Any suggestion is greatly appreciated. Thanks in advance.

/s/

Craig
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I think it's seeing TagNmbr as a cell address A12345 (Column A Row 12345)
And wold be evaluated as
=MAX(IF($C$1:$C$9=A12345,$H$1:$H$9))

As a Cell formula, that would have to be written like
=MAX(IF($C$1:$C$9="A12345",$H$1:$H$9))

Need to get those quote marks into the formula..

Try
Maxif = Application.Evaluate("=Max(IF(" & VehicleTagRange.Address & "=""" & TagNmbr & """," & MaxMilesRange.Address & "))")
 
Upvote 0
Jonmo1,

Thanks a million. That worked. I did not think that the quotes would have been the issue. I appreciate it.

/s/

Craig
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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