lightning66

Untitled

Jul 28th, 2025
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub PullDataFromDashboard()
  2.  
  3.     Dim dashboardPath As String
  4.     Dim dashboardWB As Workbook
  5.     Dim sourceWS As Worksheet
  6.     Dim destWS As Worksheet
  7.     Dim inputDate As String
  8.     Dim parsedDate As Date
  9.     Dim lastRow As Long
  10.     Dim pasteRow As Long
  11.     Dim copyRange As Range
  12.     Dim cell As Range
  13.     Dim r As Range
  14.     Dim parts() As String
  15.  
  16.     ' Prompt for MM/DD/YYYY
  17.    inputDate = InputBox("Enter the start date (MM/DD/YYYY):", "Select Start Date")
  18.  
  19.     If inputDate = "" Then
  20.         MsgBox "No date entered. Exiting."
  21.         Exit Sub
  22.     End If
  23.  
  24.     If InStr(inputDate, "/") = 0 Then
  25.         MsgBox "Invalid date format. Use MM/DD/YYYY"
  26.         Exit Sub
  27.     End If
  28.  
  29.     On Error GoTo InvalidDate
  30.     parts = Split(inputDate, "/")
  31.     parsedDate = DateSerial(parts(2), parts(0), parts(1)) ' MM/DD/YYYY ? DateSerial(YYYY, MM, DD)
  32.    On Error GoTo 0
  33.  
  34.     ' Prompt to select the Dashboard file
  35.    With Application.FileDialog(msoFileDialogFilePicker)
  36.         .Title = "Select the Dashboard Excel file"
  37.         .Filters.Clear
  38.         .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm"
  39.         If .Show <> -1 Then
  40.             MsgBox "No file selected. Exiting."
  41.             Exit Sub
  42.         End If
  43.         dashboardPath = .SelectedItems(1)
  44.     End With
  45.  
  46.     ' Open the Dashboard file
  47.    Set dashboardWB = Workbooks.Open(dashboardPath, ReadOnly:=True)
  48.     Set sourceWS = dashboardWB.Sheets("data")
  49.  
  50.     lastRow = sourceWS.Cells(sourceWS.Rows.Count, 2).End(xlUp).Row ' Column B
  51.  
  52.     ' Build copy range
  53.    For Each cell In sourceWS.Range("B2:B" & lastRow)
  54.         If IsDate(cell.Value) Then
  55.             If DateValue(cell.Value) >= parsedDate Then
  56.                 If copyRange Is Nothing Then
  57.                     Set copyRange = cell.EntireRow
  58.                 Else
  59.                     Set copyRange = Union(copyRange, cell.EntireRow)
  60.                 End If
  61.             End If
  62.         End If
  63.     Next cell
  64.  
  65.     If copyRange Is Nothing Then
  66.         MsgBox "No data found on or after " & inputDate, vbInformation
  67.         dashboardWB.Close SaveChanges:=False
  68.         Exit Sub
  69.     End If
  70.  
  71.     ' Destination sheet = active sheet in Prep file
  72.    Set destWS = ThisWorkbook.ActiveSheet
  73.     pasteRow = destWS.Cells(destWS.Rows.Count, 1).End(xlUp).Row + 1
  74.  
  75.     ' Loop through each area and copy 85 columns only
  76.    For Each r In copyRange.Areas
  77.         r.Resize(, 85).Copy Destination:=destWS.Cells(pasteRow, 1)
  78.         pasteRow = destWS.Cells(destWS.Rows.Count, 1).End(xlUp).Row + 1
  79.     Next r
  80.  
  81.     ' Close Dashboard file
  82.    dashboardWB.Close SaveChanges:=False
  83.  
  84.     MsgBox "Filtered data copied successfully from " & Format(parsedDate, "mmm dd, yyyy")
  85.     Exit Sub
  86.  
  87. InvalidDate:
  88.     MsgBox "Invalid date format. Please enter a valid date in MM/DD/YYYY format."
  89. End Sub
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment