I'm trying to create a vba function to replace multiple characters from cell value. I'm calling this function 'TestFunction' for now. The purpose of the function is to parse illegal characters from the data to be able to use it and the original data needs to remain in their own columns after parse.
I have pasted my code and table as a reference.
To use the function I would like to use =TestFunction(...) on a helper column on the same row as the data is in. The code works for a single reference e.g. =TestFunction(A1), but when trying to pass concatenated reference e.g. =TestFunction(A1&A2) it returns #VALUE!.
I've tried to fix this for a while with no success. I could make another helper column where the concatenated data is but I would really love to get this function to work without additional column.
Anyone know if this is possible to achieve?
Reference code:
Function TestFunction(CellContents As Range) As String
Dim CellTextReplaced As String
Dim char As Variant
' Characters to be replaced with "|" as delimiter
Const SpecialCharacters As String = ".|,|!| |/|\|+|-|@|&"
' Replace all special characters
CellTextReplaced = CellContents.Value
For Each char In Split(SpecialCharacters, "|")
CellTextReplaced = Replace(CellTextReplaced, char, "")
Next
' Output
TestFunction = CellTextReplaced
End Function
Reference table:
| A | B | C | D |
-------------------------------------------------------------------
1 | .test | .test | =TestFunction(A1) | =TestFunction(A1&B1) |
2 | ,test | ,test | =TestFunction(A2) | =TestFunction(A2&B2) |
3 | test- | test- | =TestFunction(A3) | =TestFunction(A3&B3) |
4 | /test\ | /test\ | =TestFunction(A4) | =TestFunction(A4&B4) |

UDF()to accept aStringrather than aRange.