Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
I'm adapting a data validation sub that utilizes Ranges instead to use an Array. Already, the time savings are substantial, but I'm hung up on one last aspect.
Below is one of the original functions I used. The code before the CASE statement I've handled and the first part of the CASE statement has been replaced with a Find/Replace function for the array (thanks, shg); however, I'm stuck at the CASE ELSE.
Below is shg's Find/Replace function, using an array (rra_FindReplace) based off of a table with Find and Replace terms in each of the columns
My questions are these: 1) using the the terms in rra_FindReplace(irow_FR, 2) {the Replace portion of the table}, how can I identify any remaining elements that don't fit the replacement terms (ie, the CASE ELSE of the original function)? The downside of using the original Find/Replace array is that the second column contains duplicate data (three values for U).
2) how can I use the original F/R table instead of creating a new table without duplcates for the Collection (if a Collection is the way to go)??
I've created a 1D array without duplicates of the Replace terms, created a collection, and then used MATCH/INDEX, but all that gets me is an error one cell below the end of the table/used range within the Sex column (duplicated three times, as that's the number in terms in the Collection), and ignores all other real errors. Here's what I've kludged together so far.
Most importantly, I need to avoid those cases where the sub labels "F" as being wrong when it's evaluating the "M" term. I was hoping to roll the solution into the Find/Replace function above.
Any help would be most appreciative. Thanks y'all
Below is one of the original functions I used. The code before the CASE statement I've handled and the first part of the CASE statement has been replaced with a Find/Replace function for the array (thanks, shg); however, I'm stuck at the CASE ELSE.
Code:
Function fx_SEX(ByRef ths As Worksheet, _
ByRef gnrCol As range)
Dim cell As range For Each cell In gnrCol
If IsError(cell.value) = True Then
format_ErrorLog ths, cell, "err" ', gbl_tblLog_fmt
ElseIf cell = vbNullString Or cell.Value2 = "NULL" Then
cell = vbNullString
If Not cell.MergeCells Then _
format_ErrorLog ths, cell, "null" ', gbl_tblLog_fmt
Else
Select Case cell.value
Case "M", "Male": cell.Value2 = "M"
Case "F", "Female": cell.Value2 = "F"
Case "U", "UNK", "Unknown": cell.Value2 = "U"
[COLOR=#ff0000][B]Case Else[/B][/COLOR]
format_ErrorLog ths, cell, "err"
End Select
End If
Next cell
End Function
Below is shg's Find/Replace function, using an array (rra_FindReplace) based off of a table with Find and Replace terms in each of the columns
Code:
Sub arr_replace(rra_data As Variant, _
icol As Long, _
rra_FindReplace As Variant, _
ths As Worksheet)
' ~~ Find/Replace All equivalent for arrays
' [URL]http://www.mrexcel.com/forum/excel-questions/983164-replace-all-equivalent-arrays.html#3[/URL]
Dim irow_FR As Long, _
irow_arr As Long
For irow_FR = LBound(rra_FindReplace) To UBound(rra_FindReplace)
For irow_arr = LBound(rra_data, 1) To UBound(rra_data, 1)
If UCase(rra_data(irow_arr, icol)) = UCase(rra_FindReplace(irow_FR, 1)) Then
rra_data(irow_arr, icol) = rra_FindReplace(irow_FR, 2)
End If
Next irow_arr
Next irow_FR
End Sub
My questions are these: 1) using the the terms in rra_FindReplace(irow_FR, 2) {the Replace portion of the table}, how can I identify any remaining elements that don't fit the replacement terms (ie, the CASE ELSE of the original function)? The downside of using the original Find/Replace array is that the second column contains duplicate data (three values for U).
2) how can I use the original F/R table instead of creating a new table without duplcates for the Collection (if a Collection is the way to go)??
I've created a 1D array without duplicates of the Replace terms, created a collection, and then used MATCH/INDEX, but all that gets me is an error one cell below the end of the table/used range within the Sex column (duplicated three times, as that's the number in terms in the Collection), and ignores all other real errors. Here's what I've kludged together so far.
Code:
' ~~ Convert" the array to a collection (unique list of Replace items)
For i = 1 To UBound(arr_coll, 1)
On Error Resume Next
coll.Add arr_coll(i, 1), CStr(arr_coll(i, 1))
Next i
' [URL="http://stackoverflow.com/a/7740341"]Excel vba - Compare two ranges and find non matches - Stack Overflow[/URL]
For i = 1 To coll.count
If Application.WorksheetFunction.Match(coll(i), Application.WorksheetFunction.index(rra_data(), 0, icol), 0) = "#N/A" Then _ ' [COLOR=#0000ff][B]<-- I just need the IF statement to evaluate properly![/B][/COLOR]
format_ErrorLog ths, ths.Cells(irow, icol), "err" '<-- this is just "do something" and works fine as is
Next i
Most importantly, I need to avoid those cases where the sub labels "F" as being wrong when it's evaluating the "M" term. I was hoping to roll the solution into the Find/Replace function above.
Any help would be most appreciative. Thanks y'all
Last edited: