Date Problem in User Form Search

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
185
Office Version
  1. 365
Platform
  1. Windows
I use the following code to ensure that a time is correctly formatted in a TextBox.

Private Sub TextBox1_Afterupdate()
Dim tString As String
With TextBox1
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tString = Left(tString, 2) & ":" & Right(tString, 2)

TextBox1.Value = Format(TimeValue(tString), "HH:MM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm")
End If
End With
End Sub

However, when I try to recall a record to the User Form using the time as the search value I get the message that the search value cannot be found. To test the search functionality I changed the code to search for a different record. The search actually functions as expected with the exception that the time value returned is 00:01.

I have set the cell format for this record to custom format "hh:mm".

Example
Keyed Value in User Form = 1234
AfterUpdate Value = 12:34
Value displayed in Cell = 12:34
Value displayed in Formula Bar = 12:34:00
Value returned in User Form = 00.01 (when search is performed using a different search criteria)

I am assuming that the reason why the search by "time" does not work is because the actual cell value and the displayed cell value are different.

Unfortunately, the "time" is the only unique value and is the only suitable record by which to make a search. In addition, this means that if the record is subsequently updated, then this value will be incorrect.

As ever, any help is greatly appreciated.
 
Thank you for your help with this..
By resetting the original code , using brief code to format the date information and using .Text instead of .Value when recalling the details everything now works as I had hoped.
 
Upvote 0
Solution

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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