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
265 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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot, both solutions provided by Rorya and The Barman works perfectly well..
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 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

12 Experts available now in Live!

Get 1:1 Help Now