Convert Text Box string to date (VBA Excel 2003)

DeusXv

Well-known Member
Joined
Jul 15, 2013
Messages
618
I'm wondering how you convert a string that a user enters into a text box to a date

so if I'm using this line of code to take the users input

Code:
 If .Value = CDate(DateSearch.TextBox1.Text) Then
                    .Offset(0, 1).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)

I want the text that they enter (01/01/2012) to be converted to a date so that it can be compared to other dates.

Thanks In advance

- DeusXv
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Yeah I've tried that however what happens is it only copies one of the dates

So if the user enters the date 01/2001 they then want the code to copy all dates that have that month and year so if i have the dates

01/01/2001
02/01/2001
03/01/2001

The code only copies the first date and pastes it to sheet2 and not the other dates. looking around people said that converting the string that they enter to a date would allow me to look up month values and year values.

Do I maybe need to format the textbox as I have no lines of code for the textbox in the userform.
 
Upvote 0
Your two messages look to me like two different questions! You have the answer to the first, but please could you expand on what you require in the second?

Please be clear about what the user is inputting and what the output you require is and how the output should flow from the input.
 
Upvote 0
So this is the basic Idea behind the code

For example:

01/03/201310
23/04/20132
04/04/201312
09/01/201312

<tbody>
</tbody>


User inputs '04/2013' and the code searches through the above list of dates and returns the value 14.

What I have read is that I need to convert what the user inputs to a date so that I can compare it to the list of dates and then copy what matches the criteria that they have typed in the textbox.

 
Upvote 0
Here is the full code that I'm using so you can see how I've attempted to go about the above.

Code:
Sub SearchMacro()
DateSearch.Show


Dim LR As Long, i As Long
  With Sheets("Sheet1")
       LR = .Range("A" & Rows.Count).End(xlUp).Row
        For i = 1 To LR
            With .Range("A" & i)
                If .Value = DateValue(DateSearch.TextBox1.Text) Then
                    .Offset(0, 1).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
                End If
            End With
        Next i
    End With


If DateSearch.TextBox1.Text = ("01/2001") Then
Worksheets("Sheet2").Range("B3") = Application.Sum(Sheets("Sheet2").Columns("A:A"))


ElseIf DateSearch.TextBox1 = ("02/2001") Then
Worksheets("Sheet2").Range("C3") = Application.Sum(Sheets("Sheet2").Columns("A:A"))

End if

End Sub

The if's that I added at the end are just to decide where the adding of the dates goes depending on the month that they have selected.

Am I trying to tackle this is a real backwards fashion or is there a far simpler way to tackle this issue ?
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,222
Members
448,951
Latest member
jennlynn

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