Adding a time combobox to a form

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I am not a coder, just a regular guy trying to use VBA to help me automate some tasks at work :)

I have this form that I am using to collect data, there is a combobox I am using for user to pick the time. I tried finding an easy date and time picker but was not able to find so I am having the user just type it in.

It works fine however, it seems like there is a more efficient way to write this code or to solve for the problem of picking the time, any suggestions?

VBA Code:
If ComboBoxTimes.value = "9am" Then
ActiveSheet.Range("O" & lRow) = "9:00"
Else
If ComboBoxTimes.value = "10am" Then
ActiveSheet.Range("O" & lRow) = "10:00"
Else
If ComboBoxTimes.value = "11am" Then
ActiveSheet.Range("O" & lRow) = "11:00"
Else
If ComboBoxTimes.value = "12pm" Then
ActiveSheet.Range("O" & lRow) = "12:00"
Else
If ComboBoxTimes.value = "1pm" Then
ActiveSheet.Range("O" & lRow) = "13:00"
Else
If ComboBoxTimes.value = "2pm" Then
ActiveSheet.Range("O" & lRow) = "14:00"
Else
If ComboBoxTimes.value = "3pm" Then
ActiveSheet.Range("O" & lRow) = "15:00"
Else
If ComboBoxTimes.value = "4pm" Then
ActiveSheet.Range("O" & lRow) = "16:00"
End If
End If
End If
End If
End If
End If
End If
End If
1676404120182.png
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Show your events (Sub or Function) lines otherwise we could be suggesting what you're already using. I suggest combo AfterUpdate event and one line:
ActiveSheet.Range("O" & lRow) = ComboBoxTimes

That assumes the bound column of your combo contains those values (which it looks like they don't). In that case, easiest would probably be to add the equal 24 hour values beside the ones you've shown. You don't have to show them. Then refer to that column and use those values:
ActiveSheet.Range("O" & lRow) = ComboBoxTimes.Column(1)

Column count is zero based, so 1 is 2nd column.
 
Upvote 0
How about

VBA Code:
ActiveSheet.Range("O" & lRow) = CDate(ComboBoxTimes.value)
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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