I am working on a code for my company that would take our production relief sheets and compile the data into one big document for compiling. I have a code that runs and does what I need it do; however, I have run into a problem. Our relief sheets are saved to our ShareDrive with the name of the current date (example: "4-27-2022"). Our site does not work on Saturdays or Sundays; therefore, we do not have relief sheets for Saturdays or Sundays. The code below works for importing data from a Monday-Friday, but it doesn't work if you want to look at dates that are separated by a weekend.
Is there a way to get excel to run my current code but skip the dates that do not have a file?
code:
Option Explicit
Sub ImportPolymerData()
'This sub imports the data from the polymer relief sheets based on the date range specified.
Dim StartDate As String, EndDate As String, SMonth As Integer, EMonth As Integer, d As String, dt As String
Dim StartY As String, SShortY As String, EndY As String, ShortEndY As String
Dim diff As Integer
Dim wbRS As Workbook 'Relief Sheet workbook
Dim wb As Workbook
Dim rng As Range
Dim r As Range
Dim dat As String
Dim i As Integer
Set wb = ThisWorkbook
Dim myPath As String
Dim myFile As String
Dim Dmonth As Integer
Application.ScreenUpdating = False
wb.Sheets("Inputs").Activate
StartDate = Range("B4").Value 'Pulls the start and end date out of cells B4 and B5
EndDate = Range("B5").Value
SMonth = Month(StartDate) 'Pulls the Month of the start and end date
EMonth = Month(EndDate)
StartY = Year(StartDate) 'Pulls the Year of the start and end date
EndY = Year(EndDate)
SShortY = Right(StartY, 2) 'Pulls the last two numbers of the year of the start and end date
ShortEndY = Right(EndY, 2)
'''Update FilePath after New Year'''
myPath = "\\cx.championx.com\AMER\US-Garyville\Groups\Champion X Operators\Polymer Relief Sheets\Polymer Relief Sheet Current\" & StartY
'Get date range difference
diff = DateDiff("d", StartDate, EndDate) 'Counts the number of days between the start and end date
'This will be the end of the loop for looping through the files
'MsgBox diff
'Clear Old Data
wb.Sheets("5B Polymer").Activate
wb.Sheets("5B Polymer").Range(Range("A2").End(xlToRight), Range("A2").End(xlDown)).Clear
On Error Resume Next 'tells excel to skip lines of code containing errors
'Loop through files
For i = 0 To diff 'From 0 to the # of days in the range of start date to end date
dat = wb.Sheets("Inputs").Range("B4").Value + i
Dmonth = Month(dat) 'Gets the month for whatever date you have going through the loop
'Format "dat" to find the Polymer Relief Sheet file
dt = Format(dat, "mm/dd/yyyy") 'Changes the date format to match the way dates are entered as relief sheet titles
d = Replace(dt, "/", "-")
'MsgBox d
'Opens the Relief sheet folder with the month of the dates
Select Case Dmonth
Case 1
myFile = myPath & "\01-JAN\" & d & ".xlsm"
Case 2
myFile = myPath & "\02-FEB\" & d & ".xlsm"
Case 3
myFile = myPath & "\03-MAR\" & d & ".xlsm"
Case 4
myFile = myPath & "\04-APR\" & d & ".xlsm"
Case 5
myFile = myPath & "\05-MAY\" & d & ".xlsm"
Case 6
myFile = myPath & "\06-JUN\" & d & ".xlsm"
Case 7
myFile = myPath & "\07-JUL\" & d & ".xlsm"
Case 8
myFile = myPath & "\08-AUG\" & d & ".xlsm"
Case 9
myFile = myPath & "\09-SEP\" & d & ".xlsm"
Case 10
myFile = myPath & "\10-OCT\" & d & ".xlsm"
Case 11
myFile = myPath & "\11-NOV\" & d & ".xlsm"
Case 12
myFile = myPath & "\12-DEC\" & d & ".xlsm"
End Select
'Open up
Set wbRS = Workbooks.Open(myFile)
'Unhides all hidden worksheets in the relief sheet workbook
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
'Get polymer data
wbRS.Sheets("5B Batches").Activate
Application.CutCopyMode = False
'Range(range("A2").end (xltoright), range("A2").end(xldown)).select
Range(Range("A2").End(xlToRight), Range("A2").End(xlDown)).Copy
With wb.Sheets("5B Polymer")
If .Range("A2") = "" Then
.Range("A2").PasteSpecial Paste:=xlPasteValues
Else
.Range("A2").End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
End If
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy"
End With
Application.CutCopyMode = False
'save and close
wbRS.Close False
Next i
wb.Sheets("5B Polymer").Activate
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy"
Range("A2").Select
Sheets("Inputs").Range("I2").Value = Now
End Sub
Any help is greatly appreciated!