Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. Public Sub PromptUserForInputDates()
  2.  
  3. Dim strStart As String, strEnd As String, strPromptMessage As String
  4. Dim LastOccupiedRowNum As String, LastOccupiedColNum As String
  5. Dim strLocation As String
  6.  
  7.  
  8. strStart = InputBox("Please enter the start date (mm/dd/yyyy)")
  9.  
  10. If Not IsDate(strStart) Then
  11. strPromptMessage = "Not Valid Date"
  12.  
  13. MsgBox strPromptMessage
  14.  
  15. Exit Sub
  16. End If
  17.  
  18. strEnd = InputBox("Please enter the end date (mm/dd/yyyy)")
  19.  
  20. If Not IsDate(strStart) Then
  21. strPromptMessage = "Not Valid Date"
  22.  
  23. MsgBox strPromptMessage
  24.  
  25.  
  26. Exit Sub
  27. End If
  28.  
  29. strLocation = InputBox("Please Enter the Location")
  30.  
  31. If strLocation = Empty Then
  32. MsgBox strPromptMessage
  33.  
  34. Exit Sub
  35. End If
  36.  
  37. Call CreateSubsetWorksheet(strStart, strEnd, strLocation)
  38.  
  39.  
  40.  
  41.  
  42.  
  43. End Sub
  44.  
  45.  
  46.  
  47.  
  48. Public Sub CreateSubsetWorksheet(StartDate As String, EndDate As String, Location As String)
  49.  
  50.  
  51.  
  52. Dim wksData As Worksheet, wksTarget As Worksheet
  53. Dim lngLastRow As Long, lngLastCol As Long, lngDateCol As Long
  54. Dim rngFull As Range, rngResult As Range, rngTarget As Range
  55. Dim lngLocationCol As Long
  56.  
  57.  
  58.  
  59. Set wksData = ThisWorkbook.Worksheets("Sheet1")
  60. lngDateCol = 4
  61. lngLocationCol = 21
  62.  
  63.  
  64. lngLastRow = LastOccupiedRowNum(wksData)
  65. lngLastCol = LastOccupiedColNum(wksData)
  66. With wksData
  67. Set rngFull = .Range(.Cells(1, 1), .Cells(lngLastRow, lngLastCol))
  68. End With
  69.  
  70. With rngFull
  71. .AutoFilter Field:=lngDateCol, _
  72. Criteria1:=">=" & StartDate, _
  73. Criteria2:="<=" & EndDate _
  74.  
  75. With rngFull
  76. .AutoFilter Field:=lngLocationCol, _
  77. Criteria1:=Location
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. If wksData.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count = 1 Then
  85.  
  86. MsgBox "Dates Filter out all data"
  87.  
  88. wksData.AutoFilterMode = False
  89. If wksData.FilterMode = True Then
  90. wksData.ShowAllData
  91. End If
  92. Exit Sub
  93.  
  94. Else
  95.  
  96. Set rngResult = .SpecialCells(xlCellTypeVisible)
  97.  
  98. Set wksTarget = ThisWorkbook.Worksheets.Add
  99. Set rngTarget = wksTarget.Cells(1, 1)
  100. rngResult.Range("A1:A5000").Copy Destination:=rngTarget
  101. Worksheets("Sheet1").Range("A1:A5000").Copy
  102. rngTarget.Range("A1").PasteSpecial Transpose:=True
  103.  
  104.  
  105.  
  106. End If
  107. End With
  108. End With
  109.  
  110.  
  111. wksData.AutoFilterMode = False
  112. If wksData.FilterMode = True Then
  113. wksData.ShowAllData
  114.  
  115. End If
  116.  
  117. MsgBox "Data Transferred"
  118.  
  119. End Sub
  120.  
  121. Public Function LastOccupiedRowNum(Sheet As Worksheet) As Long
  122. Dim lng As Long
  123. If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
  124. With Sheet
  125. lng = .Cells.Find(What:="*", _
  126. After:=.Range("A1"), _
  127. Lookat:=xlPart, _
  128. LookIn:=xlFormulas, _
  129. SearchOrder:=xlByRows, _
  130. SearchDirection:=xlPrevious, _
  131. MatchCase:=False).Row
  132.  
  133. End With
  134. Else
  135. lng = 1
  136. End If
  137. LastOccupiedRowNum = lng
  138. End Function
  139.  
  140. Public Function LastOccupiedColNum(Sheet As Worksheet) As Long
  141.  
  142. Dim lng As Long
  143. If Application.WorksheetFunction.CountA(Sheet.Cells) <> 0 Then
  144. With Sheet
  145. lng = .Cells.Find(What:="*", _
  146. After:=.Range("A1"), _
  147. Lookat:=xlPart, _
  148. LookIn:=xlFormulas, _
  149. SearchOrder:=xlByColumns, _
  150. SearchDirection:=xlPrevious, _
  151. MatchCase:=False).Column
  152.  
  153. End With
  154. Else
  155. lng = 1
  156. End If
  157. LastOccupiedColNum = lng
  158.  
  159. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement