MAX IF Formula in VBA

prajul89

Active Member
Joined
Jul 9, 2011
Messages
404
I have this formula in Excel which works perfectly
{=MAX(IF(A:A=K1,J:J))}

Where it finds out the maximum value in J column for every K1-value in A column

But I want to Use this formula in Macro.

Of course I can write a lengthy Macro which would Loop and get the result,
but I want a one line code for the above formula.
Is it possible???
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Suppose you want to enter this formula in range B1 then in VBA:
Code:
Range("B1").FormulaArray = "=MAX(IF(A:A=K1,J:J))"
 
Upvote 0
Well Actually I want to get the result in the macro itself! I don't want to use a cell in it. Is there some code for that like application.worksheetsfunction or something???:confused:
 
Upvote 0
Maybe I did not understand:

Here you pass formula to a cell then get the result assigned.
Code:
Public Sub Test()
Dim myVar As Long
Range("B1").FormulaArray = "=MAX(IF(A:A=K1,J:J))"
myVar = Range("B1").Value
Range("B1").Value = ""
End Sub

Or do it within VBA like:
Code:
Public Sub Test2()
Dim myVar As Long
myVar = Evaluate("=MAX(IF(A:A=K1,J:J))")
End Sub
 
Upvote 0
its nice helping code snippet you shared here!

I want to ask something more about

how can I pass a VBA form value to this formula string?
 
Upvote 0
its nice helping code snippet you shared here!

I want to ask something more about

how can I pass a VBA form value to this formula string?

Welcome to MrExcel.

Maybe you should've started a new thread which would have given quicker response.

You could write something like this (untested)

Bold part you will have to change to suit.
Rich (BB code):
Dim myVar As Long 
myVar = Evaluate("=MAX(IF(A:A=" & uf1.tbVariable.Value & ",J:J))")
 
Upvote 0
Maybe I did not understand:

Here you pass formula to a cell then get the result assigned.
Code:
Public Sub Test()
Dim myVar As Long
Range("B1").FormulaArray = "=MAX(IF(A:A=K1,J:J))"
myVar = Range("B1").Value
Range("B1").Value = ""
End Sub

Or do it within VBA like:
Code:
Public Sub Test2()
Dim myVar As Long
myVar = Evaluate("=MAX(IF(A:A=K1,J:J))")
End Sub

Is there anyway to copy the result for above VBA to value with code? (additional code for above)
 
Upvote 0
BayExcel'e hoş geldiniz.

Belki de daha hızlı tepki verecek yeni bir konu başlatmalıydın.

Böyle bir şey yazabilirsiniz (test edilmemiş)

Kalın kısmı uygun şekilde değiştirmeniz gerekecektir.
Code:
Dim myVar Uzun
myVar = Değerlendir("=MAKS(EĞER(A:A=" ve [B]uf1.tbVariable.Value[/B] & ",J:J)")
Bilgi için teşekkürler.
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,486
Members
452,917
Latest member
MrsMSalt

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