Flexibilized array editing
Though you want to append non-matching search strings only to the end
of a given 1-dim array,
it might be fun to study a function in addition to FaneDuru's valid solution which allows to insert, too
if you pass an index argument localizing where to position the new item.
This approach profits from a virtual (combo) control and its '.Add` method.
- If you only want to append a new item like in OP, it suffices to reassign
arr = NewItem(arr, srch).
- If you want, however to insert an item at the third position (by using a 0-based index), pass the wanted index as further argument
arr = NewItem(arr, srch, 2) or arr = NewItem(arr, srch, idx:=2).
Sub ExampleCall()
Dim arr: arr = Split("A,B,V,N,G,Y,U,Q,O,S,Z", ",")
Dim srch: srch = "W"
'1) check for matching position
Dim mtch: mtch = Application.Match(srch, arr, 0)
'2) append non-matching item
If Not IsNumeric(mtch) Then arr = NewItem(arr, srch)
Debug.Print "arr(" & LBound(arr) & " To " & UBound(arr) & ") = " & Join(arr, "|")
' ~~> arr(0 To 11) = A|B|V|N|G|Y|U|Q|O|S|Z|W
End Sub
Function NewItem()
Excpects a 1-dim array as input argument; otherwise you'd need to change sections c) and d).
Function NewItem(arr, ByVal insert As String, _
Optional idx As Variant, Optional rebase As Boolean = True)
'Purpose: - input of 0-based idx argument: inserts item
' - no input of idx argument: appends item
With CreateObject("Forms.ComboBox.1")
'a) assign array to list property (no Userform ctrl needed!)
.List = arr
'b) append or insert?
If IsMissing(idx) Then
.AddItem insert ' append item
Else
.AddItem insert, idx ' insert item (Index argument idx is zero - based!)
End If
'c) change 2-dim .List values to a "flat" array
Dim tmp: tmp = Application.Transpose(.List)
'd) return function result
If rebase Then ReDim Preserve tmp(LBound(arr) To UBound(arr) - LBound(arr) + 1)
NewItem = tmp
End With
End Function
stringToBeFound As Char, will the 'string to be found' be a single character?