I'm getting a run-time 1004 error on the .RemoveDuplicates line in the following userform initialize code when the workbook is run in shared mode. This error does not occur when the workbook is run in "unshared" mode:
Private Sub UserForm_Initialize()
Dim ContactList As Range, DedupedOrganizationList As Range
Set ContactList = Sheets("Lists").Range("Contacts")
Me.cbxContact.Text = ActiveCell
Me.cbxEmail.Text = ActiveCell.Offset(0, 1).Value
With Sheets("Lists")
.Range("I2:K100000").ClearContents
.Range("K1").Value = "Unique Company List"
.Range("I2:K" & ContactList.Rows.Count) = ContactList.Value
.Range("I:J").ClearContents
.Range("K1:K" & ContactList.Rows.Count).RemoveDuplicates Columns:=1, Header:=xlYes
End With
Me.cbxOrganization.RowSource = "DedupedOrganization"
Me.cbxOrganization.Text = ActiveCell.Offset(0, 3).Value
Run CheckActiveScreen(Me)
End Sub
When the RemoveDuplicates line is commented out, the code works without issue in shared mode.
I've read that sheet protection has caused trouble for people in this regard, but I can't find any info out there (or here) on how to handle the "shared" problem.
My objective is to populate a combo box with a deduped list of companies. Companies can be duplicated in my Contacts
named range, so I can't use that range as a source for the drop-down. My solution was to move the list into another column, dedup it, and set that as the combobox row source.
I guess I could populate an array or dictionary and dedup that (without having to use the RemoveDuplicates command), but the built-in command is just so easy to implement...if there's an easy tweak to get it working in shared mode, I'd like to try that first.
If it's not possible to use the RemoveDuplicates command in shared mode, please give me a lead on how I can dedupe an array variable or a dictionary and use the result to populate my combobox row source with that list.
Is there an alternative function out there for deduping a list (a workaround for the failing RemoveDuplicates)?
==================================================
Update...this worked:
Private Sub UserForm_Initialize()
Dim ContactList As Range, DedupedOrganizationList As Range
Set ContactList = Sheets("Lists").Range("Contacts")
Me.cbxContact.Text = ActiveCell
Me.cbxEmail.Text = ActiveCell.Offset(0, 1).Value
With Sheets("Lists")
.Range("I2:K100000").ClearContents
.Range("K1").Value = "Organization"
.Range("I2:K" & ContactList.Rows.Count) = ContactList.Value
.Range("I:J").ClearContents
Run ShareFriendlyRemoveDuplicates
End With
Me.cbxOrganization.RowSource = "DedupedOrganization"
Me.cbxOrganization.Text = ActiveCell.Offset(0, 3).Value
Run CheckActiveScreen(Me)
End Sub
Function ShareFriendlyRemoveDuplicates()
Dim dict As Object, rowCount As Long, strVal As String, lastRow As Long
Set dict = CreateObject("Scripting.Dictionary")
rowCount = Sheets("Lists").Range("K100000").End(xlUp).Row
lastRow = rowCount
Do While rowCount > 1
strVal = Sheets("Lists").Cells(rowCount, 11).Value2
If dict.exists(strVal) Then
Sheets("Lists").Cells(rowCount, 11).ClearContents
Else
dict.Add strVal, 0
End If
rowCount = rowCount - 1
Loop
Set dict = Nothing
'sort to close gaps
Sheets("Lists").Range("K2:K" & lastRow).Sort Key1:=Sheets("Lists").Range("K2"), Order1:=xlAscending, Header:=xlYes
End Function