Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

filling ID in combobox without duplicates Doesn't work

0

Hi experts,

this code was working with some project but when I use it with a new project in version office 2019 will close workbook and recovery again .

as to office 2010 will show error object variable or with block variable not set in this line

For Each x In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row).Value
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim dar As Object
Dim x

Set dar = CreateObject("System.Collections.ArrayList")


    With ws
       For Each x In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row).Value
         If Not dar.Contains(x) Then dar.Add CStr(x)
        Next
   End With

dar.Sort
Me.ComboBox1.List = dar.Toarray()


End Sub

the code supposes to fill combobox1 without duplicates ID  based on column B .

so what's wrong guys ?

Answer
Discuss

Answers

0
Selected Answer

Alaa

Don't have time to check but try removing .Value so that line looks in a range (not a single value) using:

For Each x In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row) 

Revision #1 13 July 2024

In the attached revised file, I've done the change mentioned above and also corrected the code elsewhere - see bold items and comments below:

Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim dar As Object
Dim x

Set dar = CreateObject("System.Collections.ArrayList")
' say which worksheet ws is (ActiveSheet, given it's launched from a button on it...
Set ws = ActiveSheet

    With ws
       For Each x In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row) '.Value
         ' check the cell value is in dar
         If Not dar.Contains(x.Value) Then dar.Add CStr(x.Value)
        Next
   End With

dar.Sort
Me.ComboBox1.List = dar.Toarray()


End Sub

Now, when you launch the form, ComboBox1 shows a duplicate-free list like Alaa, ALIAN and ALIIN. Note that wiithout the added qualifier x.Value, you were checking if the ArrayList dar contained the range (cell) x (not what it contains) so every cell value was added and you had several duplicates.

This file works well on both Excel 2010 and 365. I don't have 2019 to check but hope it works. (If not, it may be to do with the absence of .NET or the version of .NET on your 2019 laptop- since I think the System.Collections.ArrayList needs the .NET framework).

Hope this fixes things for you. If so, please select this Answer.

Discuss

Discussion

Hi John,
 
I tought too , but unfortunatly deosn't seem problem from it.  
Alaa (rep: 32) Jul 12, '24 at 2:05 pm
Okay Alaa, thanks. I'll try to look at it properly tomorrow. 
John_Ru (rep: 6537) Jul 12, '24 at 3:29 pm
Took a look and think I've fixed it- see Revision #1 13 July 2024 to my Answer and file.
John_Ru (rep: 6537) Jul 13, '24 at 6:32 am
Thanks John,
unfortunately  still problem continues with 2019 version 
I will check it with 2010 version when comeback to home  if the problem from .NET framework .
Alaa (rep: 32) Jul 13, '24 at 6:47 am
Okay Alaa, please let me know later  about 2010 (mine works perfectly).

I don't know much about Microsoft .NET Framework but I think some organisations have policies so that it is either not installed or limited to a particuar version. You can search the internet to see how to check the version yourself (or ask your IT Department about it). Bear in mind, that's just my suggestion of the possible cause, it might be something else.
John_Ru (rep: 6537) Jul 13, '24 at 7:10 am
works perfectly with 2010 version 
seem from NET Framework despite of it's existed with 2019 version !
anyway thank you so much.
Alaa (rep: 32) Jul 13, '24 at 12:16 pm
Thanks for selecting my Answer, Alaa. Not sure how to help further. 
John_Ru (rep: 6537) Jul 13, '24 at 5:45 pm
Add to Discussion
0

 Hello Alaa,

First I would recommend you change the title of yout post to reflect the question asked in your post. Your title would lead others to think it has to do with opening the workbook when the question you ask is about the userform code for the combobox

I could only find 2 small errors with your code.

1) You declare variable ws as a Worksheet but in your With block you don't specify which worksheet the code applies to. I changed this to:

With Sheets("AZX")

2) in your line:

For Each x In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row).Value
you have a dot before Cells; remove the dot.

After making these 2 small changes, the combobox of the userform only has 3 names in it.

If this is the rsult you are wanting please mark my answer as selected.

Cheers   :-)

Discuss

Discussion

thanks Willie,
currently I work at work's laptop contains version 2019  and still problem continues as I said
when I use it with a new project in version office 2019 will close workbook and recovery again .
Alaa (rep: 32) Jul 13, '24 at 4:11 am
@Willie - your suggestion  "you have a dot before Cells; remove the dot." works here but may not elsewhere. Removing the dot would cause VBA to determine the last used row for ws only if ws is the activesheet (which is the default). If your changed macro (setting ws and without the dot) was launched from a blank sheet (rather the AZX in the users file say), the last row would be returned as 1 (not 14 in the case for AZX with the dot)

One more thing- please see if my Answer file works for you on Excel  2007 or 2016 (which, from your Profile, you seem to run)- preferably the latter (just click the button and look at the ComboBox) and report back in the Discussion thread to my Answer. It works fine for me but the user says (works) 2019 doesn't work. Thanks in advance.
John_Ru (rep: 6537) Jul 13, '24 at 6:40 am
@John
When I first tried Alaa's original file I got run-time error 2146232576 (80131700). A quick search informed me this was related to the .NET Framework. Another search lead me to this short youtube video which clearly explains how to resolve this (the process took about 20 minutes) 
[LINK URL="Solved: Excel vba Run-time error -2146232576 (80131700) Automation error (youtube.com)"]Fix .NET Framework issue[/LINK]

After I made the small changes I mention above everything works as your modified file does. Your file works fine on my laptop with 2016. It seems the root of the problem was that the ws object was left empty; that is why I made the change to the opening With line to focus on the AZX worksheet.

Here's a curious thing. In Alaa's original file I get run-time error 91 when clicking the commandbutton but in your file - a modification of Alaa's - I don't get an error and the commandbutton code runs fine. I tried adding another commandbutton to Alaa's file with its own code and I got the same error. I can't figure this one out.

Cheers   :-)
WillieD24 (rep: 637) Jul 13, '24 at 9:58 am
@Willie - thanks for trying my Excel 2016 (after fixing. NET on your laptop) and confirming it works okay. I too spotted ws wasn't t defined but fixed it differently. 

Regarding your "curious thing... (with) Alaa's original file", I can't explain it but have seen the oroblem a long time ago (don't recall how I fixed). I got an error which debugged to the UserForm1.Show line behind it, though UserForm1 definitely existed) . Once I fixed the code, that didn't hapoen. Seems like VBA detects the error before the Initialize code runs.

@Omaran - please check the recent post avove, especially Willie's helpful tip on a runtime error and fixing. NET 
John_Ru (rep: 6537) Jul 13, '24 at 11:14 am
@John 
who is Omaran ?!
is that me ?!
Alaa (rep: 32) Jul 13, '24 at 12:43 pm
Oops, sorry Alaa. I was also replying to another user (Omaran) at the same time but in a rush. 
John_Ru (rep: 6537) Jul 13, '24 at 5:44 pm
@John
as to Willie's helpful tip on a runtime error and fixing. NET  for me I find  difficulty to see the setting on video ( the video is not clear you can say  the video is foggy).
it happened odd cases :
I unistall frame net and install again with 2019 version then your solution work but old version frame net because when  instill the last updating doesn't work .
suddenly the original code in OP works with 2010 version.

Alaa (rep: 32) Jul 14, '24 at 5:08 pm
Sorry Alaa but your words (translation?) don't make much sense. I know very little about .Net Framework so I cannot provide further help. 
John_Ru (rep: 6537) Jul 15, '24 at 4:50 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login