I am trying to copy data from a "Data" worksheet into a already created worksheet called "DateData". I want the user to be able to enter in a Start Date ("L15") and End Date ("L16") in a separate worksheet called "No Entry". On a button click...Then the data in "Data" worksheet is pulled into the "DateData" Worksheet, only including records between those dates (including the start and entry date). I hope that makes sense haha
I have tried the below but keep getting errors. The first being a "Sort method of Range class failed 1004". The code below also doesn't use the preset worksheet to copy data but creates a sheet at the end of all worksheets (which I don't want).
The "Data" worksheet has titles all in row 1 and data starts from A2 onwards...It has 19 columns of titles (so data filled) and the date that I want it looking for is in column G..G1=Title, G2 = Date starts. Date format = dd/mm/yyyy
How would I go about doing this? Any help would be so grateful. Thank you
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Dim StartDate, EndDate As Date
Dim MainWorksheet As Worksheet
StartDate = Sheets("NoEntry").Range("L15").Value
EndDate = Sheets("NoEntry").Range("L16").Value
Set MainWorksheet = Worksheets("Data")
MainWorksheet.Activate
Range("G1").CurrentRegion.Sort key1:=Range("G1"), order1:=xlAscending, Header:=xlYes
Range("G1").CurrentRegion.AutoFilter Field:=7, Criteria1:=">=" & StartDate, Operator:=xlAnd,
Criteria2:="<=" & EndDate
ActiveSheet.AutoFilter.Range.Copy
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Paste
Selection.Columns.AutoFit
Range("G1").Select
MainWorksheet.Activate
Selection.AutoFilter
Sheets("NoEntry").Activate
End Sub
So as you can see from the "Data" worksheet I have sorted the data but because it has blanks they are at the bottom (as in there are no dates in the G column for it). This was before validation so this happened
And what copies over onto the "DateData" worksheet is only the records with blank dates.
Sorry for the black filled records as they are private information. I hope that makes sense.
Dim StartDate as Date, EndDate as Date
. You have to assign each variable type individually (common mistake :) – urdearboyG
– urdearboykey = 1
– urdearboyWorksheets.Add
.... – Dean