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.
Calls a standard pre-parser function
Which would have to be rewritten or duplicated to call parse2 instead of parse 1.
I would like to rewrite it so that
and
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
Example.
Code:
Sub Main()
' Code ...
For Each cell in Range
' Code ...
preParse1(cell.value)
' Code ...
Next cell
End Sub
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
Code:
Function parse2(str as String) as String
' Code ...
End Function
Code:
Sub Main()
' Code ...
For Each cell in Range
' Code ...
preParse(cell.value, parser#)
' Code ...
Next cell
End Sub
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
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: