Workbook open using IShare Path

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
291
Office Version
  1. 2016
Platform
  1. Windows
Path to my sheets - "https://airport.ishare.tsa.dhs.gov/fieldlocations/MHT/soc/SOC Scheduling/Shared Documents/DOR"

DOR Folder is the top folder where my sheets will be found. Sub Folders are "Archived" and a monthly folder "2022.10" (that changes each month before being moved to the "Archived" folder.

What I need to get is the excel file dated the day before the current day. So if today is 10/15/22 I need to get the file named "DO Report 10-14-22". A new file is created each day. So tomorrow I will have a file named "DO Report 10-15-22" and so on.

This file could be in any of the 3 folders. ie: DOR, 2022.10, Archived. Next month one of the folders will change to 2022.11 and so on.

How do I do this using VBA? Thanks for any help.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Since you know the document name and one of it's three locations, just try each of the three locations and wrap them in an "On Error GoTo" statement.
This is a pretty bad way of handling errors but since you haven't received an answer yet it should point you in the right direction.

eg
VBA Code:
Sub openDOReport()
    Dim wbMyWorkbook As Workbook
    Dim strWBName As String
    Dim strWBPathStub As String
    Dim strWBPath As String

    strWBName = "DO Report " & Format(DateAdd("d", -1, Date()), "yyyy-dd-mm") & ".xlsx" 'yesterday's DO report
    strWBPathStub = "https://airport.ishare.tsa.dhs.gov/fieldlocations/MHT/soc/SOC Scheduling/Shared Documents/DOR"

    'Attempt 1
   strWBPath = strWBPathStub
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub 

    'Attempt 2
   strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date()), "yyyy-dd-mm")
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub 

   'Attempt 3
   strWBPath = strWBPathStub & "/Archive"
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub 

   MsgBox "Failed to locate " & strWBName

End Sub
 
Upvote 0
Since you know the document name and one of it's three locations, just try each of the three locations and wrap them in an "On Error GoTo" statement.
This is a pretty bad way of handling errors but since you haven't received an answer yet it should point you in the right direction.

eg
VBA Code:
Sub openDOReport()
    Dim wbMyWorkbook As Workbook
    Dim strWBName As String
    Dim strWBPathStub As String
    Dim strWBPath As String

    strWBName = "DO Report " & Format(DateAdd("d", -1, Date()), "yyyy-dd-mm") & ".xlsx" 'yesterday's DO report
    strWBPathStub = "https://airport.ishare.tsa.dhs.gov/fieldlocations/MHT/soc/SOC Scheduling/Shared Documents/DOR"

    'Attempt 1
   strWBPath = strWBPathStub
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub

    'Attempt 2
   strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date()), "yyyy-dd-mm")
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub

   'Attempt 3
   strWBPath = strWBPathStub & "/Archive"
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub

   MsgBox "Failed to locate " & strWBName

End Sub
This is what I got to work. Thank you for your help.

VBA Code:
Sub openDOReport()

    Dim wbMyWorkbook As Workbook
    Dim strWBName As String
    Dim strWBPathStub As String
    Dim strWBPath As String

    strWBName = "DO Report " & Format(DateAdd("d", -1, Date), "mm-dd-yy") & ".xlsm"   'yesterday's DO report
    strWBPathStub = "https://airport.ishare.tsa.dhs.gov/fieldlocations/MHT/soc/SOC Scheduling/Shared Documents/DOR"

    'Attempt 1
   strWBPath = strWBPathStub
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub

    'Attempt 2
   strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date), "mm-dd-yy")
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub

   'Attempt 3
   strWBPath = strWBPathStub & "/Archive"
   On Error Resume Next
   Set wbMyWorkbook = Workbooks.Open(strWBPath & "\" & strWBName)
   If Not wbMyWorkbook Is Nothing Then Exit Sub

   MsgBox "Failed to locate " & strWBName

End Sub
 
Upvote 0
Oppps, spoke too soon. So it seems that if my file is in the actual "DOR" Folder it will find it. If my file is in the "Archive" Folder it will find it. But if it's in the monthly folder....(ie: 2022.11) it won't find it. That folder changes each month......2022.12, then it will start with 2023.01.....and so on..... After each month those monthly folders move to the Archive folder.
 
Upvote 0
So attempt 2 is failing. Probably because we wrote "yyyy-dd-mm" instead of "yyyy.mm"

Instead of writing
VBA Code:
strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date()), "yyyy-dd-mm")
try writing
VBA Code:
strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date()), "yyyy.mm")
If that doesn't work modify your
VBA Code:
Format(DateAdd("d", -1, Date()), "yyyy-dd-mm")
line until you get it to match up with the folder name.

An easy way to do this is to go into the VBA editors immediate window and type
VBA Code:
Debug.Print Format(DateAdd("d", -1, Date()), "yyyy.mm")
and play around with that until you get the correct format.
 
Upvote 0
Solution
So attempt 2 is failing. Probably because we wrote "yyyy-dd-mm" instead of "yyyy.mm"

Instead of writing
VBA Code:
strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date()), "yyyy-dd-mm")
try writing
VBA Code:
strWBPath = strWBPathStub & "/" & Format(DateAdd("d", -1, Date()), "yyyy.mm")
If that doesn't work modify your
VBA Code:
Format(DateAdd("d", -1, Date()), "yyyy-dd-mm")
line until you get it to match up with the folder name.

An easy way to do this is to go into the VBA editors immediate window and type
VBA Code:
Debug.Print Format(DateAdd("d", -1, Date()), "yyyy.mm")
and play around with that until you get the correct format.
Yep, that was the problem. Thank You!!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top