Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub date_choice()
- Dim i As Long, j As Long, x As Long
- Dim sDt As Date, eDt As Date
- Dim end_date_last_occurence As Long
- Dim strt_date_1st_occurence_row(1 To 500000) As Variant
- 'On Error Resume Next
- Raw_data_last_row = Range("A" & Rows.Count).End(xlUp).Row 'Get the last row no
- Sheets(1).Name = "Raw Data"
- start_Date = InputBox("Enter Start Date", "Start Date", "dd,mm,yy")
- end_Date = InputBox("Enter End Date", "End Date", "dd,mm,yy")
- a = Split(start_Date, ",")
- b = Split(end_Date, ",")
- Yr_strt = Val(a(2))
- Mth_strt = Val(a(1))
- Dy_strt = Val(a(0))
- sDt = DateSerial(Yr_strt, Mth_strt, Dy_strt)
- 'MsgBox sDt
- Yr_end = Val(b(2))
- Mth_end = Val(b(1))
- Dy_end = Val(b(0))
- eDt = DateSerial(Yr_end, Mth_end, Dy_end)
- 'MsgBox eDt
- 'Check that END_DATE must be greater than START_DATE
- If eDt < sDt Then
- MsgBox "End Date Must be Greater than Start Date"
- Exit Sub
- End If
- i = 1
- For x = 1 To Raw_data_last_row
- If sDt <= Cells(x, 22) And Cells(x, 22) <= eDt Then
- strt_date_1st_occurence_row(i) = x
- '"i" is also an indicator of how many times eDt occurs
- 'i = 1 initially. Total row will be row_1st_occur + i
- i = i + 1
- End If
- Next
- 'Find the last relevant row
- end_date_last_occurence = strt_date_1st_occurence_row(1) + i - 2
- ActiveWorkbook.Sheets.Add
- ActiveSheet.Name = "Edited Data"
- 'Copy the relevant raw data
- Sheets("aaa").Select
- Range("A" & strt_date_1st_occurence_row(1) & ":" & "BE" & end_date_last_occurence).Select
- Selection.Copy
- 'Paste the relevant raw data to another worksheet
- Sheets("Edited Data").Select
- Last_row = Range("A" & Rows.Count).End(xlUp).Row
- Range("A" & Last_row).Select
- ActiveSheet.Paste
- Sheets("aaa").Rows("1:1").Copy
- Sheets("Edited Data").Range("A" & ActiveCell.Row).Insert
- Application.CutCopyMode = False
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement