How to make OptionButton in Userform autoselect based on the date and time - userform

I have a created a Userform that auto-populates the Date and Time into a TextBox (TextBox2).
What I want is for the OptionButtons (OptionButton 5 = Start Shift) (OptionButton6 = End Shift) to auto select based on the date and time from TextBox2. I am only focused on the time, the date isn't as important.
I want OptionButton5 to autoselect if the time is equal to or before 10AM.
I want OptionButton6 to autoselect if the time is equal to or after 10:01AM.
I wasn't sure if this was possible. Ive been looking for any code I could find, and haven't had any luck.

Something like the code below will help you achieve what you are wanting, this will enter the date and time into Textbox1 when the Userform is Initialized, then under the Textbox1_Change method, it will check whether it is before or after 10:00 and set the option buttons accordingly:
Private Sub TextBox1_Change()
Dim val As Variant
val = Format(TextBox1.Value, "hh:mm")
If val > "10:00" Then
OptionButton1.Value = True
Else
OptionButton2.Value = True
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1.Value = Format(Now(), "dd/mm/yyyy hh:mm")
End Sub

Related

How to add an AM/PM option at the end of a custom cell format?

In order to allow for quick time entry, I have a custom format of
0":"00
so that entering 516 will result in 5:16. This works fine but I'd like to add whether it is in AM or PM.
I've tried adding AM/PM to the end of this custom format
0":00 AM/PM
so ideally, it would display as 5:16 PM if the cell was filled in in the PM. Unfortunately, it only ever shows AM, regardless of what time it was entered.
Does anyone know how I can get this functionality? Maybe a custom format isn't the option I'm looking for?
Say we are entering values in column A. If we enter:
516
in A1 and it is morning when we make the entry, we want Excel to convert the value to:
5:16 AM
If it is afternoon when the entry is made, we want the value to convert to 5:16 PM
Place the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim trail As String, v As String
With Target
If Intersect(Range("A:A"), Target) Is Nothing Then Exit Sub
If .Value = "" Then
Application.EnableEvents = False
.Clear
Application.EnableEvents = True
Exit Sub
End If
trail = Right(CStr(Now()), 2)
v = .Text
mins = Right(v, 2)
hrs = Mid(v, 1, Len(v) - 2)
If trail = "PM" Then hrs = hrs + 12
Application.EnableEvents = False
.Clear
.Value = TimeSerial(hrs, mins, 0)
.NumberFormat = "[$-en-US]h:mm AM/PM;#"
Application.EnableEvents = True
End With
End Sub
This will result in true Excel time values with the correct format.

Set a timer between two button clicks

I am using an Excel file with an Userform to control how long it takes for some people to complete a set of tasks.
I have an Userform that opens on start up and as soon as you click the button on it, the time starts counting. However, I don't really understand how I can do that.
I need the timer to run until the task is complete and the user clicks "Exit" or "Complete" and then save the time to my spreadsheet.
I propose slightly different approach. In the userform, declare variables:
Private startTime As Double
Private endTime As Double
Private blTime As Boolean
blTime is double that tells the application that the clock goes on. Then set value for startTime when userform is launched:
Private Sub UserForm_Initialize()
startime = Time
blTime = True
End Sub
I have assumed thet your button complete control is named cbComplete. Change to your button's real name
Private Sub cbComplete_Click()
If blTime Then endTime = Time
blTime = False
End Sub
The clock is turn off (blTime = False) when user click the button. You can add analogous code for other buttons as well but only the first click matters.
What we need is a way to get time that lapsed from the form. Use property:
Property Get Timespan() As Double
If Not blTime Then
Timespan = endTime - startTime
Else
Timespan = 0
End Property
Then, in module you can use command UserForm1.Timespan. Or format it nicely:
Format(UserForm1.Timespan, "hh:mm:dd")
Important: UserForm cannot be .Unload before, just .Hide.
You could adapt something like this:
Sub CalculateRunTime_Minutes()
'PURPOSE: Determine how many minutes it took for code to completely run
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim StartTime As Double
Dim MinutesElapsed As String
'Remember time when macro starts
StartTime = Timer
'*****************************
'Insert Your Code Here...
'*****************************
'Determine how many seconds code took to run
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
'Notify user in seconds
MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation
End Sub
To suit your needs. Source is here
You can learn more about VBA's Timer function here
A second option would be to use the Now function which will give you the current time. So for example:
t = Now
.... run your tasks....
tfinal = Now
time_elapsed = tfinal - t

How to get a label caption to be set in VBA?

This is my first time with creating a UserForm.
I've looked for solutions, but as yet have not found one that works.
I am trying to set the caption for a couple of labels. I want them set when the form opens.
I have this right now(In the UserForms code:
Private Sub Budget_Initialize()
Me.Label25.Caption = Format(Month(Now), "mmmm")
Me.Label26.Caption = Format(Month(Now) + 1, "mmmm")
Me.Caption = "Test, Test, Test!"
Me.Repaint
End Sub
But it doesn't work.
I tried this too(In the ThisWorkbook code):
Private Sub Workbook_Open()
Budget.Show
With Budget
.Label25.Caption = Format(Month(Now), "mmmm")
.Label26.Caption = Format(Month(Now) + 1, "mmmm")
.Caption = "Test, Test, Test!"
End With
Budget.Repaint
End Sub
It opens the UserForm when I open the workbook, but it doesn't update the captions.
I want the captions to be the names of this month and next month.
What am I doing wrong?
The event routine you need (independent of the name of the form)
Private Sub UserForm_Initialize()
....
End Sub
So basically, just rename Budget_Initialize() to UserForm_Initialize()
Format(Month(Now), "mmmm") will always be January, as it is maximally Format(12, "mmmm"). Skip month, Now alone is what you need.

Excel Add-in with some info stored

I have created a UserForm1 in Excel and saved it as an add-in. This add-in works fine but it does not store some data that I need (does not store it in itself not in the opened excel). I have to store some information in cells A1 and A2 (in A1 Username, in A2 today's date).
When I run this add-in the UserForm1 does not contain these values.
Is there a way how I can store the UserName and get the updated date?
Here is the code for UserForm1:
Private Sub UserForm1_Initialize()
Me.DocumentName.Text = ActiveWorkbook.FullName
DocumentName.Visible = False
TextBoxDate.Value = Worksheets("Sheet1").Cells(2, "A").Value
TextBoxDate.Value = CDate(TextBoxDate.Value)
UserName.Visible = False
Userform1.UserName.Text = CStr(Range("A1").Value)
'If A1 is empty pops up a UserRegister form
If UserName = "" Then
UserRegister.Show
End If
End Sub
UserRegister form code:
Private Sub UserName_Change()
Sheets("Sheet1").Range("A1") = UserName.Text
End Sub
' I want to store the UserName, so the user does not have to enter it every single time
Private Sub CommandButtonGO_Click()
ThisWorkbook.Save
Unload Me
End Sub
To get the date I just use the formula =TODAY() in Cell A2. I know there are other ways, but I found this one very simple.
Can you try this?
UserForm UserForm1:
Private Sub UserForm1_Initialize()
Me.DocumentName.Text = ActiveWorkbook.FullName
DocumentName.Visible = False
TextBoxDate.Value = ThisWorkbook.Worksheets("Sheet1").Range("A2").Value
'TextBoxDate.Value = CDate(TextBoxDate.Value)
UserName.Visible = False
UserForm1.UserName.Text = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
'If A1 is empty pops up a UserRegister form
If Len(UserName.Text) = 0 Then
UserRegister.Show
End If
Debug.Print "Name: " & ThisWorkbook.Worksheets("Sheet1").Range("A1").Value
Debug.Print "Date: " & ThisWorkbook.Worksheets("Sheet1").Range("A2").Value
End Sub
UserForm UserRegister:
Private Sub UserName_Change()
CommandButtonGO.Enabled = Not (UserName.Text = ThisWorkbook.Worksheets("Sheet1").Range("A1").Value)
End Sub
' I want to store the UserName, so the user does not have to enter it every single time
Private Sub CommandButtonGO_Click()
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Trim(UserName.Text)
ThisWorkbook.Worksheets("Sheet1").Range("A2").Value = Now
ThisWorkbook.Save
Unload Me
End Sub
Private Sub UserRegister_Initialize()
UserName.Text = UCase(Environ("USERNAME"))
End Sub
Well, I figured out how I can do it.
To get a user name i used a code from here : Getting computer name using VBA (It also says how to get User Name there)
To get current date of entry I just changed the code for output to:
ActiveCell.Offset(1, 0).Select 'Date column A
ActiveCell.Value = Date
And it outputs the current date in my LOGfile excel.
Thanks a lot for your help =)

Formatting dates in a combobox dropdown list

I have created a simple userform with a combobox populated with a range of dates (rngWeekList) but I am having serious headaches trying to get the list in the dropdown box to appear in "dd-mmm-yy" format. Here is my code:
Private Sub UserForm_Initialize()
' Populate the list with the date range
ComboBox1.List = Worksheets("Cover").Range("rngWeekList").Value
' Set the defulat selection (based off rngWeekIndex)
ComboBox1.ListIndex = Worksheets("Cover").Range("rngWeekIndex").Value - 1
' Format
ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub
Private Sub ComboBox1_Change()
' Format
ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub
This manages to format the selected item in the combobox correctly (e.g. "02-Jul-14") but when I open the dropdown list, all the list entries shown are formatted in the default "m/d/yyyy". Is there a way to change the formatting for the list entries? It is confusing for users who are used to seeing the day before the month.
Thanks in advance for your help, it is much appreciated.
Ed
I managed to fix it by looping through each item in the comboboax and formatting it (feel free to correct me if there is a more elegant way to do it!)
Private Sub UserForm_Initialize()
Dim i As Integer
' Populate the list with the date range
ComboBox1.List = Worksheets("Cover").Range("rngWeekList").Value
'Format all items
For i = 0 To ComboBox1.ListCount - 1
ComboBox1.List(i) = Format(DateValue(ComboBox1.List(i)), "dd-mmm-yy")
Next i
' Set the default selection (based off rngWeekIndex)
ComboBox1.ListIndex = Worksheets("Cover").Range("rngWeekIndex").Value - 1
End Sub
Private Sub ComboBox1_Change()
' Format the selection
ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub
Sorry for posting, but I really thought I was stuck.
Thanks again,
Ed