Advertisement
Guest User

Untitled

a guest
May 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub date_choice()
  2.  
  3.  
  4. Dim i As Long, j As Long, x As Long
  5. Dim sDt As Date, eDt As Date
  6. Dim end_date_last_occurence As Long
  7. Dim strt_date_1st_occurence_row(1 To 500000) As Variant
  8. 'On Error Resume Next
  9. Raw_data_last_row = Range("A" & Rows.Count).End(xlUp).Row 'Get the last row no
  10. Sheets(1).Name = "Raw Data"
  11. start_Date = InputBox("Enter Start Date", "Start Date", "dd,mm,yy")
  12. end_Date = InputBox("Enter End Date", "End Date", "dd,mm,yy")
  13. a = Split(start_Date, ",")
  14. b = Split(end_Date, ",")
  15. Yr_strt = Val(a(2))
  16. Mth_strt = Val(a(1))
  17. Dy_strt = Val(a(0))
  18. sDt = DateSerial(Yr_strt, Mth_strt, Dy_strt)
  19. 'MsgBox sDt
  20. Yr_end = Val(b(2))
  21. Mth_end = Val(b(1))
  22. Dy_end = Val(b(0))
  23. eDt = DateSerial(Yr_end, Mth_end, Dy_end)
  24. 'MsgBox eDt
  25. 'Check that END_DATE must be greater than START_DATE
  26. If eDt < sDt Then
  27.     MsgBox "End Date Must be Greater than Start Date"
  28.     Exit Sub
  29. End If
  30.  
  31. i = 1
  32. For x = 1 To Raw_data_last_row
  33. If sDt <= Cells(x, 22) And Cells(x, 22) <= eDt Then
  34. strt_date_1st_occurence_row(i) = x
  35. '"i" is also an indicator of how many times eDt occurs
  36. 'i = 1 initially. Total row will be row_1st_occur + i
  37. i = i + 1
  38. End If
  39. Next
  40. 'Find the last relevant row
  41. end_date_last_occurence = strt_date_1st_occurence_row(1) + i - 2
  42. ActiveWorkbook.Sheets.Add
  43. ActiveSheet.Name = "Edited Data"
  44. 'Copy the relevant raw data
  45. Sheets("aaa").Select
  46. Range("A" & strt_date_1st_occurence_row(1) & ":" & "BE" & end_date_last_occurence).Select
  47. Selection.Copy
  48. 'Paste the relevant raw data to another worksheet
  49. Sheets("Edited Data").Select
  50. Last_row = Range("A" & Rows.Count).End(xlUp).Row
  51. Range("A" & Last_row).Select
  52. ActiveSheet.Paste
  53. Sheets("aaa").Rows("1:1").Copy
  54. Sheets("Edited Data").Range("A" & ActiveCell.Row).Insert
  55. Application.CutCopyMode = False
  56. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement