clear/reset the dependent combobox in userform

dgwan

New Member
Joined
May 4, 2022
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
command button = clmClear
first combobox = AreaBox
second combobox = RoomBox


Private Sub AreaBox_Change()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("data")
Dim i As Integer
Dim n As Integer

n = Application.WorksheetFunction.Match(Me.AreaBox.Value, sh.Range("A1:H1"), 0)

Me.RoomBox.Clear
For i = 2 To Application.WorksheetFunction.CountA(sh.Cells(1, n).EntireColumn)
Me.RoomBox.AddItem sh.Cells(i, n).Value

Next i


End Sub

Private Sub cmbClear_Click()
Me.AreaBox = ""
Me.RoomBox = ""
End Sub


(help it reset the Room box but not Area and it gives me an error)
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,
welcome to forum

See if this update to your code does what you want

VBA Code:
Private Sub AreaBox_Change()
    Dim sh          As Worksheet
    Dim i           As Long
    Dim n           As Variant
    
    Set sh = ThisWorkbook.Sheets("data")
    
    n = Application.Match(Me.AreaBox.Value, sh.Range("A1:H1"), 0)
    
    If Not IsError(n) Then
        Me.RoomBox.Clear
        For i = 2 To Application.WorksheetFunction.CountA(sh.Cells(1, n).EntireColumn)
            Me.RoomBox.AddItem sh.Cells(i, n).Value
        Next i
    End If
    
End Sub

Dave
 
Upvote 0
Solution
im having an error and it highlights this

n = Application.Match(Me.AreaBox.Value, sh.Range("A1:H1"), 0)
 
Upvote 0
You need to let us know the error you are getting but as a guess error 13 type mismatch? if this is so, you need to use all the code as published in #post 2

Dave
 
Upvote 0
Private Sub AreaBox_Change()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("data")
Dim i As Integer
Dim n As Integer

n = Application.Match(Me.AreaBox.Value, sh.Range("A1:H1"), 0)

If Not IsError(n) Then
Me.RoomBox.Clear
For i = 2 To Application.WorksheetFunction.CountA(sh.Cells(1, n).EntireColumn)
Me.RoomBox.AddItem sh.Cells(i, n).Value
Next i
End If

End Sub

(and yes its error 13 type mismatch, additional to that i cant use the submit button for AreaBox too)
 
Upvote 0
but that's not the complete code I posted

Copy the entire code as published in #post 2 & try it again

Dave
 
Upvote 0
i used other variables for that so i only copied the lower part but thanks a lot. that solved my problem even the submit part :)
 
Upvote 0
Glad update resolved your issue & appreciate your feedback

As friendly future tip, when seeking help always use the FULL code helpers here post & test it unaltered. If you then continue to have problems, feedback the errors you are getting to the person helping you.

Using only part of any suggested solutions or making your own changes the code updates, can as you discovered, lead to other issues & take longer to resolve.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,404
Messages
6,124,715
Members
449,184
Latest member
COrmerod

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