Clear Userform but doesn't clear CombiBox

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi all, good morning i have made the code below which works well but it doesn't clear the combiboxes. can you advise please.

'---------------------------------------------------------------------------------------
' DateTime : 09/05/2007 08:43
' DateTime : 09/05/2007 08:43
' Author : Roy Cox (royUK)
' Website : click here for more examples and Excel Consulting
' Purpose : Loops through controls of UserForm & clears setting
'
' Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.


'---------------------------------------------------------------------------------------
Option Explicit


Public Function ClearAll(frm As MSForms.UserForm) As Boolean
' clear out all controls
Dim Octrl As Control
' if any error occurs, just exit
On Error GoTo ExitFunc


' loop through controls, figure out type and
' use appropriate method to clear it
For Each Octrl In frm.Controls
Select Case TypeName(Octrl)
Case "TextBox": Octrl = Empty
Case "CheckBox", "OptionButton": Octrl.Value = False
Case "ComboBox", "ListBox": Octrl.ListIndex = -1
Case Else:
End Select
Next Octrl


ExitFunc:
Set Octrl = Nothing
End Function

VBA Code:
Private Sub CommandButton2_Click()
ClearAll Me
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this:

VBA Code:
Public Function ClearAll(frm As MSForms.UserForm) As Boolean
  ' clear out all controls
  Dim Octrl As Control
  ' if any error occurs, just exit
  On Error GoTo ExitFunc
  
  
  ' loop through controls, figure out type and
  ' use appropriate method to clear it
  For Each Octrl In frm.Controls
  Select Case TypeName(Octrl)
  Case "TextBox": Octrl = Empty
  Case "CheckBox", "OptionButton": Octrl.Value = False
  Case "ListBox": Octrl.ListIndex = -1
  Case "ComboBox"
    Octrl.ListIndex = -1
    Octrl = Empty
  Case Else:
  End Select
  Next Octrl
  
  
ExitFunc:
  Set Octrl = Nothing
End Function

Regards
Dante Amor
:giggle:
 
Upvote 0
Solution
Try this:

VBA Code:
Public Function ClearAll(frm As MSForms.UserForm) As Boolean
  ' clear out all controls
  Dim Octrl As Control
  ' if any error occurs, just exit
  On Error GoTo ExitFunc
 
 
  ' loop through controls, figure out type and
  ' use appropriate method to clear it
  For Each Octrl In frm.Controls
  Select Case TypeName(Octrl)
  Case "TextBox": Octrl = Empty
  Case "CheckBox", "OptionButton": Octrl.Value = False
  Case "ListBox": Octrl.ListIndex = -1
  Case "ComboBox"
    Octrl.ListIndex = -1
    Octrl = Empty
  Case Else:
  End Select
  Next Octrl
 
 
ExitFunc:
  Set Octrl = Nothing
End Function

Regards
Dante Amor
:giggle:
That works great thank you
 
Upvote 0
That works great thank you
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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