Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2012, 05:59 AM
Registered User
 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default giving out GetIf()

being tired of using V/Hlookup, Match, Index etc plus some things extremely hard to do (many results of same occurrence) , I have written GetIf()

feel free to use it, comment it etc, just keep me informed of any suggested changes-fixes

it is self explained and if I can post an archive somewhere I could demonstrate some usages.
For now:
Code:
' GETIF
' syntax: getif(range1;criteria;[range2];[ArraySize])
' now: A:range1 = range to look into for match (could be range/ranges/array/name)
'      B:criteria = lime to look for
'      C:range2 Optional: If range2 exists then GetIf will return items from range2 (range/array...) where
'                           ...criteria matches range1
'                       if range2 is missing then GetIf returns the indexes of matches found
'      ArraySize Optional: Default =  1 : return 1 result
'                          special = 0 : find quantity of matches and return as many results
'                          normal 1 to...: find as many as exist in an array as big as Asked!
' * When expecting results in array of N elements from GetIf, it is especially useful to send an
'   Arraysize of N since it will return 0 elemements in empty slots while (0) or a
'   smaller number will return #N/A in empty slots.
' * GetIf will always return a Vertical Array of elements according to ArraSize {getif(N,1)}. Can be used normaly
'   for one result (default) without Array experience. Use Traspose() of Excel to convert to Horizontal
'   ex: transpose( GetIf(N,1) ) = GetIf(1,N)
' * TIPS: Use with excel formulas: Offset(),Index(),Smaller(),Larger(),Rows(1:N),Columns(1:N) for unlimited usage.
'   See examples
'
' EDIT_3 (Jun 2012): Few MAJOR changes.
' Working and returning Vertical Arrays. HOWEVER Inputs no need to be Vertical Arrays or Arrays at all...
' Added "Optional ArraySize As Long = 1"
'
' EDIT_2 (Jun 2012): range2 made Optional. If range2 is missing then return
' the Counter found so that range1(Counter)=criteria, then use Counter with Offset or Index functions!!!
' Obsolete: GoRandom has gone bye bye... complicated things
'
' EDIT_1
' (criteria) became (criteria as string) as results where not always as anticipated. Seems to function properly now
' Osbolete: GoRandom added for specific randomize purposes
'
' ORIGINAL (Apr 2008)
' Syntax exactly like SUMIF, that is "GetIf (Range1;Criteria;Range2)"
' and finds range2(i) so that range1(i)=criteria
' No controls/checks, no speed or other improvements made by function
'
' Apostolos55
'
Public Function GetIf(A, B As String, Optional C, Optional ArraySize As Long = 1)
Dim Counter As Long, Counter2 As Long, Counter3 As Long, dum1, matchFL As Boolean
Dim Counters() As Long, Holder()

' Fix ArraySize
If ArraySize = 0 Then
    Counter = 0: For Each dum1 In A
        If B = Trim(dum1) Then Counter = Counter + 1
    Next: ArraySize = Counter: End If
If ArraySize < 1 Then ArraySize = 1     ' Restore to 1 even if evaluate finds 0 to avoid errors

' properly allocate arrays for VERTICAL results.
ReDim Counters(1 To ArraySize, 1 To 1), Holder(1 To ArraySize, 1 To 1)

Counter = 0: Counter2 = 0: Counter3 = 0
For Each dum1 In A                  ' Search for index
    Counter = Counter + 1: matchFL = False
    If B = Trim(dum1) Then matchFL = True
    If matchFL Then
        Counter3 = Counter3 + 1
        Counters(Counter3, 1) = Counter         'Log found locations to Counters()
        If Counter3 = ArraySize Then Exit For   'Search only as many as asked for
    End If
Next

If Counter3 > 0 Then                        ' if something was found
    If IsMissing(C) Then                    ' Edit:Jun2012 Ommitting range2 (c) returns Position within range
        GetIf = Counters
    Else
        Counter = 1
        For Each dum1 In C                  ' Use found locations to find equivalent
            Counter2 = Counter2 + 1
            If Counter2 = Counters(Counter, 1) Then
                Holder(Counter, 1) = dum1
                Counter = Counter + 1
                If Counter > Counter3 Then Exit For 'Search only as many as asked for
            End If
        Next
        GetIf = Holder
    End If
Else
    GetIf = "-":
End If

End Function
lots of comments in the beginning, essential to understand few things

just keep in mind, V/Hlookup, Match etc may not be 100% replaced but Getif, not even close. But Simple database usage for 1 or many results is made extremely simple.
enjoy...

Last edited by Apostolos55; July 11th, 2012 at 06:03 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
giving style to Menu majeedkhan BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 February 23rd, 2010 11:25 AM
RenderControl is Giving the Error. dpkbahuguna ASP.NET 3.5 Basics 1 June 12th, 2009 02:43 PM
giving scrollbars to gridview svibuk ASP.NET 2.0 Professional 1 March 19th, 2009 02:21 PM
Giving a program a 'name' Badman3k VB.NET 2002/2003 Basics 2 September 12th, 2004 09:57 PM
Giving focus to controls KevinO ASP.NET 1.0 and 1.1 Basics 3 January 16th, 2004 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.