السلام عليكم
قم بوضع القيم 1 و 2 و 3 في النطاق من الخلية A1 حتى الخلية A3 ثم جرب الكود التالي عله يفي بالغرض
CODE
Sub Test()
Dim b
b = Convert2DArrayTo1DArray(Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value)
Call GrayCode(b)
End Sub
Function Convert2DArrayTo1DArray(arr As Variant)
Dim b(), i As Long
ReDim b(1 To UBound(arr, 1))
For i = 1 To UBound(arr, 1)
b(i) = arr(i, 1)
Next i
Convert2DArrayTo1DArray = b
End Function
Function GrayCode(Items) As String
Dim v() As Integer, f As Boolean, b As Boolean, subList As String, newSub As String, i As Integer, k As Integer, lower As Integer, upper As Integer
k = 0: b = True
lower = LBound(Items): upper = UBound(Items)
ReDim v(lower To upper)
Do Until f
newSub = ""
For i = lower To upper
If v(i) = 1 Then
If newSub = "" Then newSub = "," & Items(i) Else newSub = newSub & Items(i)
End If
Next i
subList = subList & vbCrLf & newSub
If newSub <> "" Then
k = k + 1
Cells(k, 3) = Mid(newSub, 2)
End If
If b Then
v(lower) = 1 - v(lower)
Else
i = lower
Do While v(i) <> 1
i = i + 1
Loop
If i = upper Then f = True Else i = i + 1: v(i) = 1 - v(i)
End If
b = Not b
Loop
GrayCode = subList
End Function