Custom Function: Extract All Matching Data
The purpose of this function is to act as multiple vlookups to pull all data that has multiple matches, and allows the user to specify how they want the data delimited
The function is called EXTRACTALL and it has four arguments:
Argument 1: Looup Field. This is the value that you are looking for. If it is text, make sure it is in quotes
Argument 2: Lookup Range. The range the function is going to search to find the value specified in argument 1
Argument 3: Output Range. If a match is found in the lookup range, this is what will be returned. The ranges should be the same length
Argument 4: Separator. This is how the results will be separated.
An example of the formula is below:
=EXTRACTALL(“A”, B:B,A:A,”,”)
This will look for the letter A in column B, and when there is a match the value from column A will be pulled. All values will be separated by commas.
You will note the values that correspond to A’s are 1, 3, 5, 8, and 9 which is what the result displays in cell D11. This acts effectively the same as a lookup function might with the difference being this will pull every match and put it into one cell whereas a lookup will just grab the first match and nothing else. This is more useful in the case of text values because after using this formula (especially if a comma separator is used) you can convert from a comma delimited field into multiple fields.
Below is the code:
Find the xth occurrence of a day in a month
This function is called dayx. It consists of the following arguments: occurrence number, day of the week, month, year. For example:
dayx(2,”Monday”,10,2014) or dayx(2,1,10,2014) will return the second Monday of October 2014, which is October 13, 2014
For information on how to insert a custom function, see this post.
Here is the code for this function:
———————————————————————————————————————–
Dim firstweekdayofmonth As Long
Dim currentmonth, endofmonth, firstdayofmonth As String
Select Case UCase(xday)
Case Is = “MONDAY”
xday = 1
Case Is = “TUESDAY”
xday = 2
Case Is = “WEDNESDAY”
xday = 3
Case Is = “THURSDAY”
xday = 4
Case Is = “FRIDAY”
xday = 5
Case Is = “SATURDAY”
xday = 6
Case Is = “SUNDAY”
xday = 7
End Select
‘Convert month number to name
currentmonth = Format(DateAdd(“m”, Val(xmonth) – 1, “January 1”), “mmmm”)
endofmonth = Format(WorksheetFunction.EoMonth(Format(DateAdd(“m”, Val(xmonth) – 1, “January 1”), “mmm dd, yyyy”), 0), “mmmm dd, yyyy”)
‘Determine the first day of the month
firstdayofmonth = Weekday(currentmonth & ” 1, ” & xyear, vbMonday)
‘Calculate
If xday >= firstdayofmonth Then
dayx = Format(currentmonth & ” ” & (7 * (xoccurrence – 1)) + 1 – (firstdayofmonth – xday) & ” , ” & xyear, “mmmm d, yyyy”, vbMonday)
Else
dayx = Format(currentmonth & ” ” & 7 + (7 * (xoccurrence – 1)) + 1 – (firstdayofmonth – xday) & ” , ” & xyear, “mmmm d, yyyy”, vbMonday)
End If
dayx = Format(DateAdd(“m”, 0, dayx), “mmmm dd, yyyy”)
End Function
———————————————————————————————————————–
Using Custom Functions and Macros
As far as coding goes, they key difference in a function as opposed to a sub procedure in Excel is in the way the code is executed. In a function, you enter in just like you would any other function, by typing out the name of the function and entering in any required arguments. With a sub procedure, it is typically executed via an event, a button, or shortcut key. Also with a function, the user will manually enter values for key variables, whereas for a sub procedure in most cases those will be calculated within the procedure itself.
Inserting a Sub Procedure (Macro)
If instead of a custom function you had sub procedure (macro) you wanted to copy into your code, the steps would be the same, except instead of using a formula to execute the code, you would need to assign either a shortcut key or a button.
IF Functions: Calculating Data If Criteria Is Met
Example 1