0

I have a ComboBox that populates values from a sheet. I am trying to select the first or second value in the combobox by default when the form initialize. Can anyone help me with how should I do this?

Sub Reset()
    Dim iRow As Long
    
    iRow = [Counta(Database!A:A)] ' idetifying the last row
    
    With frmForm
        .txtID.Value = ""
        .txtVName.Value = ""
        .optMale.Value = False
        .optFemale.Value = False
        
        'Default Color
        .txtID.BackColor = vbWhite
        .txtVName.BackColor = vbWhite
        .txtTimeIn.BackColor = vbWhite
        .txtPName.BackColor = vbWhite
        .cmbNationality.BackColor = vbWhite
        .cmbDept.BackColor = vbWhite
        '--------------------------------
    
        'Creating a dynamic name for Department
                  
        shDept.Range("A2", shDept.Range("A" & Application.Rows.Count).End(xlUp)).Name = "DynamicDept"
        .cmbDept.RowSource = "DynamicDept"
        .cmbDept.Value = ""
    End With
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I have edited your post. Seems like there was an `End With` missing. I have also indented your code. `I am trying to select the first or second value in the combobox by default when the form initialize` Try this `.cmbDept.Listindex = 0` or `.cmbDept.Listindex = 1` Also avoid finding the last row using `CountA`. You may want to see [THIS](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba/11169920#11169920) – Siddharth Rout Dec 05 '20 at 06:59
  • Thank you it worked perfectly and thank you for tidying up my code as well. – kamran mirza Dec 05 '20 at 07:12
  • @SiddharthRout can you also clarify If I needed to select any values from the list should I use .cmbDept.Listindex = 0,1,2,3,4? – kamran mirza Dec 05 '20 at 07:14
  • Yes depending on their position. or if you know the value then `.cmbDept.Value = "Blah Blah"` – Siddharth Rout Dec 05 '20 at 07:36

0 Answers0