Solved

Excel Custom Fuction unable to constantly show the calcuated value and requires reselection of cells in order to display result

Posted on 2013-11-12
4
288 Views
Last Modified: 2013-11-13
Hi there,

I've created this following custom function to determine the min value based on a selected criteria. The concept is similar to sumif function in Excel.

This is my code below, I'm currently have problem as I always need to reselect the cells in order for the result to be displayed. Excel is unable to store the calcuated value and will always revert to 0 after i go to other sheets to make changes to some of other data.

Can you help to check what is the problem with the code?

Function MinIf(VarMinRange As Range, VarCriteria As Variant, VarMin As Range)
Dim BoolFirstTime As Boolean
Dim IntColumn As Integer

'The purpose of this function is to find out the minimum value that matches a corresponding criteria in a range


BoolFirstTime = True

Set FindColumn = VarMin
IntColumn = FindColumn.Column

For Each VarMinRange In VarMinRange
    If VarMinRange = VarCriteria Then
        If BoolFirstTime = True Then
            BoolFirstTime = False
            MinIf = Cells(VarMinRange.Row, IntColumn)
        Else
            If Cells(VarMinRange.Row, IntColumn) < MinIf Then
                MinIf = Cells(VarMinRange.Row, IntColumn)
            End If
        End If
    End If
Next VarMinRange

End Function
0
Comment
Question by:sltan32
4 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39643817
Hi,

pls try
Function MinIf(VarMinRange As Range, VarCriteria As Variant, VarMin As Range)
Dim BoolFirstTime As Boolean
Dim IntColumn As Integer

'The purpose of this function is to find out the minimum value that matches a corresponding criteria in a range


BoolFirstTime = True

Set FindColumn = VarMin
IntColumn = FindColumn.Column

For Each VarMinRangeCell In VarMinRange
    If VarMinRangeCell = VarCriteria Then
        If BoolFirstTime = True Then
            BoolFirstTime = False
            MinIf = Cells(VarMinRangeCell.Row, IntColumn)
        Else
            If Cells(VarMinRangeCell.Row, IntColumn) < MinIf Then
                MinIf = Cells(VarMinRangeCell.Row, IntColumn)
            End If
        End If
    End If
Next VarMinRangeCell

End Function

Open in new window

Regards
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 200 total points
ID: 39644122
It's your use of Cells that causes the problem - that will always refer to the active sheet in your code. Try this:

Function MinIf(VarMinRange As Range, VarCriteria As Variant, VarMin As Range)
   Dim BoolFirstTime               As Boolean
   Dim n                           As Long
   Dim vCrit
   Dim vData
   'The purpose of this function is to find out the minimum value that matches a corresponding criteria in a range


   BoolFirstTime = True

   vData = VarMin.Value2
   vCrit = VarMinRange.Value

   For n = LBound(vCrit, 1) To UBound(vCrit, 1)
      If vCrit(n, 1) = VarCriteria Then
         If BoolFirstTime = True Then
            BoolFirstTime = False
            MinIf = vData(n, 1)
         Else
            If vData(n, 1) < MinIf Then
               MinIf = vData(n, 1)
            End If
         End If
      End If
   Next n

End Function

Open in new window

0
 
LVL 24

Accepted Solution

by:
Steve earned 300 total points
ID: 39644617
Rory is right about the cells being the issue with recalculation not occurring.

However, why not simply use MIN and IF functions:

Example:
=MIN(IF(D8:D19=F8,E8:E19))
entered using [ctrl]+[shift]+[enter]

See attached workbook for working example
MinIF.xlsx
0
 

Author Closing Comment

by:sltan32
ID: 39645528
Thanks a lot, both solutions provided by Rorya and The Barman works perfectly well..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now