Passing a Function as an Argument

Ralajer

Active Member
Joined
Jul 24, 2008
Messages
416
I am in the processing of generalizing a lot of my VBA code to remove redundant subroutines and functions to make code maintenance easier. One of the biggest cause for redundancy is having to copy and paste a subroutine because I changed a single function.

Example.
Code:
Sub Main()
    ' Code ...
    For Each cell in Range
        ' Code ...
        preParse1(cell.value)
        ' Code ...
    Next cell
End Sub
Calls a standard pre-parser function
Code:
Function preParse1(str as String) as String
    ' Many Lines of code
    ' Verify if it needs to be parsed
    parse1(str) 
    ' More code
End Function 

Function parse1(str as String) as String
    ' Code ... 
End Function
Which would have to be rewritten or duplicated to call parse2 instead of parse 1.
Code:
Function parse2(str as String) as String
    ' Code ... 
End Function
I would like to rewrite it so that
Code:
Sub Main()
    ' Code ...
    For Each cell in Range
        ' Code ...
        preParse(cell.value, parser#)
        ' Code ...
    Next cell
End Sub
and
Code:
Function preParse(str as String,  genericFuction as Function) as String
    ' Many Lines of code
    ' Verify if it needs to be parsed
    genericFuction(str) 
    ' More code
End Function
Where genericFunction can be parser1, parser2, etc.

I know that this can be done in other programming languages but I am not too clear how or if this can be done in VBA. I've done some searching but haven't found a clear answer. I hope I've made my problem clear if not let me know.

Thanks
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Thanks very straight forward. This isn't the first that run turned out to be a simple solution.
 
Upvote 0
Just to follow up with code that works for me.

Code:
Sub Main()
    ' Code ...
    parserN = "parse2"
    For Each cell In range
        cell.Value = preParse(cell.Value, parserN)
    Next cell

    parserN = "parse1"
    For Each cell In range
        cell.Value = preParse(cell.Value, parserN)
    Next cell
End Sub

Function preParse(str As String, genericFunction) as String
    ' Many Lines of code
    ' Verify if it needs to be parsed
    preParse = Run(genericFunction, str) 
    ' More code
End Function
 
Upvote 0

Forum statistics

Threads
1,223,280
Messages
6,171,162
Members
452,385
Latest member
Dottj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top