![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Posts: 3
|
I am trying to calculate a Max and Min value from an excel database in my VB code. Is there a way to calulate while specifying a criteria. For example if I want the Max value of UnitsSold for Region = "West", calculated within my VB Code using a database that resides on an Excel sheet?
Thanks in advance for any assistance. |
|
|
|
|
|
#2 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Quote:
Code:
Option Explicit
Sub FindMyMin()
Dim MyMin As Double
' I just used a cell that was way out of the way...
ActiveSheet.Range("IV1").FormulaArray = _
"=MIN(IF(B8:B13=""West"",C8:C13))"
MyMin = ActiveSheet.Range("IV1")
ActiveSheet.Range("IV1").ClearContents
' etc., etc.
End Sub
|
|
|
|
|
|
|
#3 | ||
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
Quote:
You can use the Evaluate method rather than working with a worksheet cell. MyMin = Evaluate("=MIN(IF(B8:B13=""West"",C8:C13))") Evaluate considers all the expressions as arrays, if needed, so you don't have to expressly state the .FormulaArray entry when using this. From a purist's perspective, Evaluate() is a cheat, but I really like it. Bye, Jay |
||
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
I LOVE the Evaluate Method, however it suffers from the same problem as the .FormulaArray property. The length of the string it is passed CAN NOT be longer than 255 chars... why ? really strange. Microsoft solution ?
"Don't use an array formula with more than 255 characters"... |
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Very nice!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|