Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub PullDataFromDashboard()
- Dim dashboardPath As String
- Dim dashboardWB As Workbook
- Dim sourceWS As Worksheet
- Dim destWS As Worksheet
- Dim inputDate As String
- Dim parsedDate As Date
- Dim lastRow As Long
- Dim pasteRow As Long
- Dim copyRange As Range
- Dim cell As Range
- Dim r As Range
- Dim parts() As String
- ' Prompt for MM/DD/YYYY
- inputDate = InputBox("Enter the start date (MM/DD/YYYY):", "Select Start Date")
- If inputDate = "" Then
- MsgBox "No date entered. Exiting."
- Exit Sub
- End If
- If InStr(inputDate, "/") = 0 Then
- MsgBox "Invalid date format. Use MM/DD/YYYY"
- Exit Sub
- End If
- On Error GoTo InvalidDate
- parts = Split(inputDate, "/")
- parsedDate = DateSerial(parts(2), parts(0), parts(1)) ' MM/DD/YYYY ? DateSerial(YYYY, MM, DD)
- On Error GoTo 0
- ' Prompt to select the Dashboard file
- With Application.FileDialog(msoFileDialogFilePicker)
- .Title = "Select the Dashboard Excel file"
- .Filters.Clear
- .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm"
- If .Show <> -1 Then
- MsgBox "No file selected. Exiting."
- Exit Sub
- End If
- dashboardPath = .SelectedItems(1)
- End With
- ' Open the Dashboard file
- Set dashboardWB = Workbooks.Open(dashboardPath, ReadOnly:=True)
- Set sourceWS = dashboardWB.Sheets("data")
- lastRow = sourceWS.Cells(sourceWS.Rows.Count, 2).End(xlUp).Row ' Column B
- ' Build copy range
- For Each cell In sourceWS.Range("B2:B" & lastRow)
- If IsDate(cell.Value) Then
- If DateValue(cell.Value) >= parsedDate Then
- If copyRange Is Nothing Then
- Set copyRange = cell.EntireRow
- Else
- Set copyRange = Union(copyRange, cell.EntireRow)
- End If
- End If
- End If
- Next cell
- If copyRange Is Nothing Then
- MsgBox "No data found on or after " & inputDate, vbInformation
- dashboardWB.Close SaveChanges:=False
- Exit Sub
- End If
- ' Destination sheet = active sheet in Prep file
- Set destWS = ThisWorkbook.ActiveSheet
- pasteRow = destWS.Cells(destWS.Rows.Count, 1).End(xlUp).Row + 1
- ' Loop through each area and copy 85 columns only
- For Each r In copyRange.Areas
- r.Resize(, 85).Copy Destination:=destWS.Cells(pasteRow, 1)
- pasteRow = destWS.Cells(destWS.Rows.Count, 1).End(xlUp).Row + 1
- Next r
- ' Close Dashboard file
- dashboardWB.Close SaveChanges:=False
- MsgBox "Filtered data copied successfully from " & Format(parsedDate, "mmm dd, yyyy")
- Exit Sub
- InvalidDate:
- MsgBox "Invalid date format. Please enter a valid date in MM/DD/YYYY format."
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment