Monday, May 4, 2020

Get unique values from an array and store them in another dynamically expanding array in VBA ,VBA Teacher Sourav,Kolkata 8910141720

Option Explicit

Sub uniquearray()
Dim arrdata() As String
Dim workingrange, cell As Range
Dim length As Integer
length = 0
Sheets("conditionalformattingvba").Select
Set workingrange = Range("I2").CurrentRegion
For Each cell In workingrange
length = length + 1
ReDim Preserve arrdata(length) As String
arrdata(length) = cell.Value

Next cell

'For length = 1 To UBound(arrdata)
'Debug.Print (arrdata(length))
'Next length

'MsgBox (arrdata(0))
Dim uniquearray() As String

Dim i As Integer


ReDim uniquearray(1) As String
For length = 1 To UBound(arrdata)
For i = 1 To UBound(uniquearray)

'Debug.Print tempstr1 & " and " & tempstr2
If arrdata(length) = uniquearray(i) Then

Exit For
End If




Next i
If i > UBound(uniquearray) Then
uniquearray(UBound(uniquearray)) = arrdata(length)
ReDim Preserve uniquearray(UBound(uniquearray) + 1) As String

End If


Next length
ReDim Preserve uniquearray(UBound(uniquearray) - 1) As String
For length = 1 To UBound(uniquearray)
Debug.Print (uniquearray(length))
Next length




End Sub

No comments:

Post a Comment