0

I have six variable ranges: A_backup, B_backup, C_backup, D_backup, E_backup, F_backup

Each range variable is one column with a variable number of rows (some have 3, others have 5, etc.)

I would like to take each cell from these ranges and add them to a new single column array called Combined_backups. I would also like to avoid adding a cell if it is a duplicate string value to a previously added cell.

Here's what I've tried. Running into issues with the Combined_backups.RemoveDuplicates. Should I make a new range for the combined array, apply the RemoveDuplicates, then create a new final array? Also, what's the best way to test that my Combined_backups array has actually become the array I was hoping for?

Dim Combined_backups() As Variant

'add A_backup
Dim j As Integer
j = A_backup.Rows.Count

ReDim Preserve Combined_backups(j)

For i = 0 To j - 1
    Combined_backups(i) = A_backup.Item(i + 1)
Next i

'add B_backup
Dim k As Integer
k = B_backup.Rows.Count

ReDim Preserve Combined_backups(j + k)

For i = 0 To k - 1
    Combined_backups(i) = B_backup.Item(i + 1)
Next i

'add C_backup
Dim l As Integer
l = C_backup.Rows.Count

ReDim Preserve Combined_backups(j + k + l)

For i = 0 To l - 1
    Combined_backups(i) = C_backup.Item(i + 1)
Next i

'add D_backup
Dim m As Integer
m = D_backup.Rows.Count

ReDim Preserve Combined_backups(j + k + l + m)

For i = 0 To m - 1
    Combined_backups(i) = D_backup.Item(i + 1)
Next i

'add E_backup
Dim n As Integer
n = E_backup.Rows.Count

ReDim Preserve Combined_backups(j + k + l + m + n)

For i = 0 To n - 1
    Combined_backups(i) = E_backup.Item(i + 1)
Next i

'add F_backup
Dim o As Integer
o = F_backup.Rows.Count

ReDim Preserve Combined_backups(j + k + l + m + n + o)

For i = 0 To o - 1
    Combined_backups(i) = F_backup.Item(i + 1)
Next i

'elminate duplicates from Combined_backups
Combined_backups.RemoveDuplicates

Thanks!

1 Answer 1

2

Here's a different approach using the Collection object. We first put everything into a Collection, using to our advantage its property of rejecting duplicates; then we put the collection object into a "results" array, and write it back to a worksheet. This assumes your various arrays are named ranges and not range objects, but you should be able to adapt as needed:

Option Explicit

Sub UniqueArray()
    Dim vSrc As Variant
    Dim colStrings As Collection
    Dim vVarRanges As Variant
    Dim vResults() As Variant
    Dim S As String
    Dim I As Long, J As Long, K As Long

vVarRanges = VBA.Array("A_backup", "B_backup", "C_backup", "D_backup", "E_backup", "F_backup")

Set colStrings = New Collection
On Error Resume Next 'So collection will omit any duplicates instead of causing an error
For I = 0 To UBound(vVarRanges)
    vSrc = Range(vVarRanges(I))
    For J = 1 To UBound(vSrc, 1)
        S = vSrc(J, 1)
        If Len(S) > 0 Then _
            colStrings.Add Item:=S, Key:=CStr(S)
    Next J
Next I
On Error GoTo 0

'Now create results array
ReDim vResults(1 To colStrings.Count, 1 To 1)
For I = 1 To colStrings.Count
    vResults(I, 1) = colStrings(I)
Next I

'Write the results someplace

With Worksheets("sheet4").Range("A1").Resize(rowsize:=UBound(vResults))
    .EntireColumn.Clear
    .Value = vResults
End With

End Sub
6
  • I've never played with Collections but will be sure to keep them in mind in future after seeing this answer. Commented Sep 4, 2014 at 0:34
  • Hi Ron, thanks for quick response. Still running into some issues, I'm assuming they have to do with the ranges I start with. When I run the code as is, I get a "subscript out of range" error at the ReDim vResults line. When I comment out the collection and results array sections, then simply write the vVarRanges, I get 5 cells that say "A_backup". If I unquote the A_backup, B_backup, etc. then it writes nothing. So, it seems like there is an issue pulling in the actual cells from my range variables. Thoughts? Commented Sep 5, 2014 at 19:27
  • @RonRosenfeld just had a thought, but couldn't get it to work. Tried using Union to get the A_backup, B_backup, C_backup, D_backup, E_backup, and F_backup ranges into one range called Combined_backups. then tried to pass this into the vVarRanges using Array(). couldn't get it to work, but think it might be the right track? Thanks! Commented Sep 5, 2014 at 21:30
  • @MattSchwartz You wrote you had six "variable ranges" named A-backup and so forth. I wrote "... assumed your arrays are named ranges" with those names. If your variable ranges are not so named, then you need to utilize the correct range references for those "variable ranges". And, if you want to use the code unmodified, put them as strings into the array statement. Commented Sep 6, 2014 at 1:19
  • @MattSchwartz So far as using the Union method, if you want to do that, you will need to change how you address the array into which you read it, as you would be dealing with multiple areas. I think it is simpler to just work with a single range at a time, as I did in the code. Coding is more complex when you deal with multiple areas in a single variant array, as another dimension is added for the different areas. Commented Sep 6, 2014 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.