First, in order to add all the letters inside your brackets to elements in an array, the array need to be dynamic (Dim arrayC As...) , and not static (Dim arrayC(26) As...). Also, either you use Split, or you need to add " before and after each element inside your brackets.
Second, in order to find if the value in "C8" is found inside the copied array (arrayCont) , which is Redim up to i (=3), we use the Application.Match to find the index of the matching element inside the array. If there is a "match" then we remove that element from the array.
Third I am using a Sub called "DeleteElementAt" which removes a certain element from an array, depeneds on the index of the array you want to remove.
My Code (Tested)
Option Explicit
Sub SubArrayfromArray()
Dim arrayC As Variant
Dim i As Integer 'Position in the arrayC(26)
Dim arrayCont As Variant
' this is the way to put all the strings inside the brackets in an array (type Variant) in one line of code
arrayC = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
i = 3 ' i is the value of another worksheet cell
' copy original dynamic array to destination dynamic array
arrayCont = arrayC
' redim the copied array up to 3 (from 0 to 2)
ReDim Preserve arrayCont(0 To i - 1)
' value in Range C8 >> if "B" was found inside the arrayCont >> remove it from the array
If Not IsError(Application.Match(Range("C8").Value, arrayCont, 0)) Then
Call DeleteElementAt(Application.Match(Range("C8").Value, arrayCont, 0) - 1, arrayCont)
End If
' for debug only
'For i = LBound(arrayCont) To UBound(arrayCont)
' MsgBox "Array element " & i & " Value is " & arrayCont(i)
'Next i
' Edit 1: place arrayCont result in Cells(9, 3) >> Range("C9")
Range("C9").Resize(UBound(arrayCont) + 1, 1).Value = Application.Transpose(arrayCont)
End Sub
Sub DeleteElementAt Code
Public Sub DeleteElementAt(ByVal ArrIndex As Integer, ByRef myArr As Variant)
' this sub removes a certain element from the array, then it shrinks the size of the array by 1
Dim i As Integer
' move all element back one position
For i = ArrIndex + 1 To UBound(myArr)
myArr(i - 1) = myArr(i)
Next
' shrink the array by one, removing the last one
ReDim Preserve myArr(UBound(myArr) - 1)
End Sub
arrayCisn't what you think it is - it has empty strings for the first 26 entries (it starts at index 0) and thenarrayC(26)is the full string of the alphabet. I believe you wantDim arrayC() As StringandarrayC = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", ",")Debug.Print (arrayC(26))with your code andDebug.Print (arrayC(25))with my code to see the difference.