Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to match the time and unable to get the correct code by using VBA in Excel
08:30
09:00
10:00
11:00
12:00
13:00
Above data’s in rows B1 to B6


Combobox2 is populated with Rows"B1:B6"
Combobox2 selects one of above

I try to get the row number of the same time, selected by combobox2
I used the code;
VB
Dim TheDate As Date
Dim Index As Variant
TheDate = ComboBox2.Value
Index = Application.Match(CLng(TheDate), Range("Sheet1!b1:b10"), 0)
If IsError(Index) Then
    MsgBox "Not Found"
Else
    MsgBox "Match item: " & Index
End If

Can someone help me please?

[Edited]Code was wrapped in the tags "code" - LOSMAC[/Edited]
Posted
Updated 17-Nov-11 6:25am
v3
Comments
Maciej Los 17-Nov-11 12:41pm    
Where did you place combobox - Sheet1, UserForm?

1 solution

In this case you don't need to search hour... Just use ListIndex property, like this:
VB
Private Sub CommandButton1_Click()
    MsgBox Me.ComboBox1.ListIndex + 1, vbInformation, "Index of..."
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900