Fasten code (UserForm)

Digitborn.com

Active Member
Joined
Apr 3, 2007
Messages
353
Hello,

I have a piece of code which works alright. My question is if it's possible to change the code and make it work faster. When I change the ComboBox I wait about 1-3 seconds for about 40 items in the Worksheet. I still didn't test it with more items. Here's the code:
Code:
Private Sub ComboBox1P2_Change()
    If ComboBox1P2.ListIndex = 0 Then
        ComboBox2P2.Style = fmStyleDropDownCombo
        ComboBox2P2.Value = sign
        ComboBox2P2.Enabled = False
        ComboBox3P2.Clear
        ComboBox3P2.Enabled = False
        ComboBox1P2.SetFocus
    ElseIf ComboBox1P2.ListIndex <> 0 And ComboBox1P2.ListIndex <> -1 Then
        ComboBox2P2.Style = fmStyleDropDownList
        ComboBox2P2.Enabled = True
        ComboBox3P2.Clear
        ComboBox3P2.Enabled = False
        ComboBox2P2.SetFocus
        With Worksheets("PartsData")
        For Each Combo2P2 In .Range("A2", .Range("A65536").End(xlUp))
            If Len(Me.ComboBox1P2) > 0 Then
                Dim dic As Object, i As Long, d, a, z, y
                z = Me.ComboBox1P2.Value
                Set dic = CreateObject("Scripting.Dictionary")
                dic.CompareMode = vbTextCompare
                    With .Range("A2:BJ" & .Range("A" & Rows.Count).End(xlUp).Row)
                        .Sort .Range("A2"), xlAscending
                        a = .Offset(0, 0).Resize(, 2).Value
                    End With
                For i = 1 To UBound(a, 1)
                    If Not IsEmpty(a(i, 1)) Then
                        d = Format(a(i, 1), "dd-mmm-yy")
                        If Not dic.exists(d) And a(i, 2) = z Then dic.Add d, Nothing
                    End If
                Next: y = dic.keys: Set dic = Nothing: Erase a
                If UBound(y) < 0 Then
                    With Me.ComboBox2P2
                        .Style = fmStyleDropDownCombo
                        .Value = sign
                        .Enabled = False
                    End With
                Else
                    With Me.ComboBox2P2
                        .Enabled = True
                        .Clear
                        .List = y
                    End With
                End If
            End If
        Next
        End With
    End If
End Sub
 
Tushar

I probably thought it may have been a control on the userform due to the OP's naming convention.:)

Rather confusing don't you think?

PS I did read the code.:)
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hello tusharm and Norie,

tusharm, i was blind. I used to place the loop For Each Combo2P2 In .Range("A2", .Range("A65536").End(xlUp)) when I work with the dates ComboBox and never mentioned that in the code downwards I didn't use the variable Combo2P2, but the variable d.

So, I deleted For Each Combo2P2 In .Range("A2", .Range("A65536").End(xlUp)) and everything have worked very fast.

p.s. Norie

Combo2P2 is a variable, of course. It's declared in the Declarations Section of the UserForm code window - Dim Combo2P2 As Range
I just named it Combo2P2 and it very much looks like a control, but that's my styling in naming variables and controls...

ComboBox2P2 is the control which collects the items from Combo2P2 variable.
 
Upvote 0
Digitborn

Finally realised it was a variable after a bit.:oops:
 
Upvote 0
{snip}
So, I deleted For Each Combo2P2 In .Range("A2", .Range("A65536").End(xlUp)) and everything have worked very fast.
{snip}
Glad you got that worked out.

Edit: The next part may not help much, i.e., the marginal benefit will not be as great as the elimination of the loop but still it will lead to cleaner code if nothing else.

Why sort the range? Just as before you are not making use of the sorted range subsequent to the sort.

Also, why use a dictionary? It will be cleaner (and almost certainly faster) to use Excel's Filter capability. Filter column A unique records to some other range and use that other range as the source for the combobox.
 
Upvote 0
Norie,

No, it's not confusing when you used to it. Rather it's very suitable, at least for me. The variable names such as prdFruits, qtyApples etc. distracts my attention and make me less effective when I look through code.

I know it's confusing for other ppl in forums, so maybe when I post a topic I should change the variable names...

Concerning loops can you tell me how can I loop this code with For Next:
Code:
                If .Cells(Combo2P2.Row, 32).Value <> "" And .Cells(Combo2P2.Row, 33).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 32).Value & " - " & .Cells(Combo2P2.Row, 33).Value & "%")
                If .Cells(Combo2P2.Row, 34).Value <> "" And .Cells(Combo2P2.Row, 35).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 34).Value & " - " & .Cells(Combo2P2.Row, 35).Value & "%")
                If .Cells(Combo2P2.Row, 36).Value <> "" And .Cells(Combo2P2.Row, 37).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 36).Value & " - " & .Cells(Combo2P2.Row, 37).Value & "%")
                If .Cells(Combo2P2.Row, 38).Value <> "" And .Cells(Combo2P2.Row, 39).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 38).Value & " - " & .Cells(Combo2P2.Row, 39).Value & "%")
                If .Cells(Combo2P2.Row, 40).Value <> "" And .Cells(Combo2P2.Row, 41).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 40).Value & " - " & .Cells(Combo2P2.Row, 41).Value & "%")
                If .Cells(Combo2P2.Row, 42).Value <> "" And .Cells(Combo2P2.Row, 43).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 32).Value & " - " & .Cells(Combo2P2.Row, 43).Value & "%")
                If .Cells(Combo2P2.Row, 44).Value <> "" And .Cells(Combo2P2.Row, 45).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 44).Value & " - " & .Cells(Combo2P2.Row, 45).Value & "%")
                If .Cells(Combo2P2.Row, 46).Value <> "" And .Cells(Combo2P2.Row, 47).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 46).Value & " - " & .Cells(Combo2P2.Row, 47).Value & "%")
                If .Cells(Combo2P2.Row, 48).Value <> "" And .Cells(Combo2P2.Row, 49).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 48).Value & " - " & .Cells(Combo2P2.Row, 49).Value & "%")
                If .Cells(Combo2P2.Row, 50).Value <> "" And .Cells(Combo2P2.Row, 51).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 50).Value & " - " & .Cells(Combo2P2.Row, 51).Value & "%")
                If .Cells(Combo2P2.Row, 52).Value <> "" And .Cells(Combo2P2.Row, 53).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 52).Value & " - " & .Cells(Combo2P2.Row, 53).Value & "%")
                If .Cells(Combo2P2.Row, 54).Value <> "" And .Cells(Combo2P2.Row, 55).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 54).Value & " - " & .Cells(Combo2P2.Row, 55).Value & "%")
                If .Cells(Combo2P2.Row, 56).Value <> "" And .Cells(Combo2P2.Row, 57).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 56).Value & " - " & .Cells(Combo2P2.Row, 57).Value & "%")
                If .Cells(Combo2P2.Row, 58).Value <> "" And .Cells(Combo2P2.Row, 59).Value <> "" Then ListBox1P2.AddItem (.Cells(Combo2P2.Row, 58).Value & " - " & .Cells(Combo2P2.Row, 59).Value & "%")
 
Upvote 0
Perhaps this.
Code:
For I = 32 To 58 Step 2
    With Combo2P2
        If .Cells(.Row, I).Value <> "" And .Cells(.Row, I + 1).Value <> "" Then
            ListBox1P2.AddItem (.Cells(.Row, I).Value & " - " & .Cells(.Row, I + 1).Value & "%")
        End If
    End With
Next I
By the way I have to say I do find your naming convention confusing.

The names of the variables seem to close to the names of the controls, hence my initial posts - I wasn't sure what was what.:)

Anyways, if it works for you that's fine, naming conventions are a personal preference as far as I'm concerned.:)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top