User Form For Dates

longytravel

Board Regular
Joined
Aug 2, 2011
Messages
68
I have the following code (kindly produced by Weaver)

I want to design a user form (userform2) that has two text boxes to add two dates - i want to pull the information from those two dates

How do i get the dates inputted by the user into the code?

As ever - grateful for your views

Code:
Sub Macro1()
    date1 = "01 jan 2010"
    date2 = "01 jan 2011"
    With Sheets("data")
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        With .Range("B1")
            .AutoFilter
            .AutoFilter Field:=2, Criteria1:=">=" & date1, Operator:=xlAnd _
            , Criteria2:="<=" & date2
        End With
        .Range("B1:B" & lr).EntireRow.Copy Sheets("reports").Range("A1")
    End With
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Code:
Sub Macro1()
    date1 = Me.TextBox1.Text
    date2 = Me.TextBox2.Text
    With Sheets("data")
        lr = .Cells(Rows.Count, "B").End(xlUp).Row
        With .Range("B1")
            .AutoFilter
            .AutoFilter Field:=2, Criteria1:=">=" & date1, Operator:=xlAnd _
            , Criteria2:="<=" & date2
        End With
        .Range("B1:B" & lr).EntireRow.Copy Sheets("reports").Range("A1")
    End With
End Sub

You should probably put in some validation though.
 
Upvote 0
Top stuff. Thank you.

I already have another userform with those text box names - can i just change the name to a text box number i dont have in use?

I'm new to all this so apoliogies for the stupid questions - how would validate all this?

Cheers
 
Upvote 0
Yeah you can change the Textbox names in the code.

This is one way to validate the Dates:

Code:
Private Sub TextBox1_AfterUpdate()
If Not IsDate(Me.TextBox1.Value) Then
    Me.TextBox1.Text = ""
    MsgBox "please type a valid date!"
    Exit Sub
End If
End Sub
Private Sub TextBox2_AfterUpdate()
If Not IsDate(Me.TextBox2.Value) Then
    Me.TextBox2.Text = ""
    MsgBox "please type a valid date!"
    Exit Sub
End If
End Sub

02/08/2011 would be ok.
02.08.2011 would not.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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