AllyGraham
New Member
- Joined
- Oct 6, 2014
- Messages
- 18
I have been working to check text strings in cells for invalid characters to use in a CSV data file, and I have got so far, but I want to see if I can make it any better or simpler.
If I have text in cell A1 is "abc123#", I wish to both state in a cell in the same row (say C1) the check result of "Invalid characters [#]", such that A1 is checked against a named range that contains a list of so called valid characters, and lists what the specific characters are that are invalid.
I found a formula that worked to state that there are invalid characters:
Which works well for what I need, and through taking a UDF I found, I can get the substitutions to work without being nested:
(source for UDF: Excel: find and replace multiple values at once - Ablebits.com)
But this looks to be volatile, and I do not really need the second column as each substitution of a character becomes "".
I feel that I can make this simpler, and I also feel I had a method in my previous job that used a non-nested substitution formula without UDF to substitute a large array down to "" again to highlight or count for invalid characters, but I do not have access to this workbook any longer.
Can anyone suggest a simple non-volatile non-nested scalable method to flag that the cell contains invalid characters and list each invalid character?
If I have text in cell A1 is "abc123#", I wish to both state in a cell in the same row (say C1) the check result of "Invalid characters [#]", such that A1 is checked against a named range that contains a list of so called valid characters, and lists what the specific characters are that are invalid.
I found a formula that worked to state that there are invalid characters:
Excel Formula:
=IF(LEN(A1)=0,"",REPT("Invalid characters"&SUBSTITUTE((" ["&MassReplace(A1,ValidList,ValidList2)&"]")," []",""),
SUMPRODUCT(0+(ISNUMBER(MATCH(MID(A1,ROW(INDEX(A:A,1):INDEX(D:D,LEN(A1))),1),ValidList,0))))<>LEN(A1))&"")
VBA Code:
Function MassReplace(InputRng As Range, FindRng As Range, ReplaceRng As Range) As Variant()
Dim arRes() As Variant 'array to store the results
Dim arSearchReplace(), sTmp As String 'array where to store the find/replace pairs, temporary string
Dim iFindCurRow, cntFindRows As Long 'index of the current row of the SearchReplace array, count of rows
Dim iInputCurRow, iInputCurCol, cntInputRows, cntInputCols As Long 'index of the current row in the source range, index of the current column in the source range, count of rows, count of columns
cntInputRows = InputRng.Rows.Count
cntInputCols = InputRng.Columns.Count
cntFindRows = FindRng.Rows.Count
ReDim arRes(1 To cntInputRows, 1 To cntInputCols)
ReDim arSearchReplace(1 To cntFindRows, 1 To 2) 'preparing the array of find/replace pairs
For iFindCurRow = 1 To cntFindRows
arSearchReplace(iFindCurRow, 1) = FindRng.Cells(iFindCurRow, 1).Value
arSearchReplace(iFindCurRow, 2) = ReplaceRng.Cells(iFindCurRow, 1).Value
Next
'Searching and replacing in the source range
For iInputCurRow = 1 To cntInputRows
For iInputCurCol = 1 To cntInputCols
sTmp = InputRng.Cells(iInputCurRow, iInputCurCol).Value
'Replacing all find/replace pairs in each cell
For iFindCurRow = 1 To cntFindRows
sTmp = Replace(sTmp, arSearchReplace(iFindCurRow, 1), arSearchReplace(iFindCurRow, 2))
Next
arRes(iInputCurRow, iInputCurCol) = sTmp
Next
Next
MassReplace = arRes
End Function
But this looks to be volatile, and I do not really need the second column as each substitution of a character becomes "".
I feel that I can make this simpler, and I also feel I had a method in my previous job that used a non-nested substitution formula without UDF to substitute a large array down to "" again to highlight or count for invalid characters, but I do not have access to this workbook any longer.
Can anyone suggest a simple non-volatile non-nested scalable method to flag that the cell contains invalid characters and list each invalid character?