1

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
MBB70
  • 375
  • 2
  • 16
  • I just tried using the Advanced Filter method...same error. Works in unshared mode but not in shared mode. – MBB70 Apr 10 '20 at 19:26
  • Going to try this ... https://stackoverflow.com/questions/11870095/remove-duplicates-from-array-using-vba – MBB70 Apr 10 '20 at 19:29
  • 2
    I'd go with deduping through dictionary. Should you need to stay as you are you may be trying what in [this link](https://www.ozgrid.com/forum/index.php?thread/6326-solved-vba-turn-sharing-off-and-on-with-vba-code/) – HTH Apr 10 '20 at 19:29
  • Dictionary worked great! No need to temporarily unshare. – MBB70 Apr 10 '20 at 21:44
  • Please do not spam other questions with a link back to yours. – DCCoder Dec 26 '20 at 20:06
  • @DCCoder I'm not clear on what you mean. This is my question and I linked to someone else's question in the comments to indicate the approach I was going to take to solve it. Is that not ok? Also, the link that HTH provided is not a SO item and was not a self-promotion...so again, I'm not sure what rules were breached. – MBB70 Jan 07 '21 at 16:21

0 Answers0