Cannot Find Correct O365 Sharepoint Path For Excel VBA

tkrupka

New Member
Joined
Feb 19, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I've been trying to create files from Excel to our company SharePoint site on Office 365, so far without success. Here is the code:

VBA Code:
Dim dYear As Double
Dim sYear As String
Dim sDC01 As String
Dim sDC02 As String
Dim sDC04 As String
Dim sDC05 As String
Dim sDC06 As String
Dim sDC11 As String
Dim sDC21 As String
Dim sGenTotals As String
Dim sDC01FileTemplate As String
Dim sDC02FileTemplate As String
Dim sDC04FileTemplate As String
Dim sDC05FileTemplate As String
Dim sDC06FileTemplate As String
Dim sDC11FileTemplate As String
Dim sDC21FileTemplate As String
Dim sGenTotalsFileTemplate As String
Dim sDC01FileNew As String
Dim sDC02FileNew As String
Dim sDC04FileNew As String
Dim sDC05FileNew As String
Dim sDC06FileNew As String
Dim sDC11FileNew As String
Dim sDC21FileNew As String
Dim sGenTotalsFileNew As String

Sub cmdCreateNewFiles_Click()
    
    'Verify that the year entered is appropiate
    
    dYear = CInt(Worksheets("Home").txtYear.Value)
    
    sYear = Worksheets("Home").txtYear.Value
    
        If Len(sYear) <> 4 Then
        
            MsgBox "Enter a Four-Digit Year.", vbCritical, "Improper Year Value Entered"
            
            GoTo Release
        
        Else
        
            If dYear < 2000 Or dYear > 2100 Then
            
                MsgBox "Enter an appropriate Four-Digit Year.", vbCritical, "Improper Year Value Entered"
            
                GoTo Release
            
            End If
        
        End If
    
    dYear = 0
    
    'Create the new directories
    
    sDC01 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC1\"
    sDC02 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC2\"
    sDC04 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC4\"
    sDC05 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC5\"
    sDC06 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC6\"
    sDC11 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC11\"
    sDC21 = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC21\"
    sGenTotals = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\DC Generator Totals\"

    If Dir(sDC01 & sYear, vbDirectory) = "" Then

        MkDir sDC01 & sYear

    End If

    If Dir(sDC02 & sYear, vbDirectory) = "" Then

        MkDir sDC02 & sYear

    End If

    If Dir(sDC04 & sYear, vbDirectory) = "" Then

        MkDir sDC04 & sYear

    End If

    If Dir(sDC05 & sYear, vbDirectory) = "" Then

        MkDir sDC05 & sYear

    End If

    If Dir(sDC06 & sYear, vbDirectory) = "" Then

        MkDir sDC06 & sYear

    End If

    If Dir(sDC11 & sYear, vbDirectory) = "" Then

        MkDir sDC11 & sYear

    End If

    If Dir(sDC21 & sYear, vbDirectory) = "" Then

        MkDir sDC21 & sYear

    End If
    
    'Create the new files
    
    sDC01FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC01 Template.xlsm"
    sDC02FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC02 Template.xlsm"
    sDC04FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC04 Template.xlsm"
    sDC05FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC05 Template.xlsm"
    sDC06FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC06 Template.xlsm"
    sDC11FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC11 Template.xlsm"
    sDC21FileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\DC21 Template.xlsm"
    sGenTotalsFileTemplate = "\\CompanyName.sharepoint.com\sites\Amer-DC-SouthCampusEng\Shared Documents\Campus Compliance\Generator Run and Training Logs\New Building Templates\Generator Totals Template - On Campus.xlsx"
    sDC01FileNew = sDC01 & sYear & "\DC01.xlsm"
    sDC02FileNew = sDC02 & sYear & "\DC02.xlsm"
    sDC04FileNew = sDC04 & sYear & "\DC04.xlsm"
    sDC05FileNew = sDC05 & sYear & "\DC05.xlsm"
    sDC06FileNew = sDC06 & sYear & "\DC06.xlsm"
    sDC11FileNew = sDC11 & sYear & "\DC11.xlsm"
    sDC21FileNew = sDC21 & sYear & "\DC21.xlsm"
    sGenTotalsFileNew = sGenTotals & "\Generator Totals - On Campus " & sYear & ".xlsx"
    
    FileCopy sDC01FileTemplate, sDC01FileNew
    FileCopy sDC02FileTemplate, sDC02FileNew
    FileCopy sDC04FileTemplate, sDC04FileNew
    FileCopy sDC05FileTemplate, sDC05FileNew
    FileCopy sDC06FileTemplate, sDC06FileNew
    FileCopy sDC11FileTemplate, sDC11FileNew
    FileCopy sDC21FileTemplate, sDC21FileNew
    FileCopy sGenTotalsFileTemplate, sGenTotalsFileNew
    
    'Modify the new file
    
    Workbooks.Open (sGenTotals & "\Generator Totals - On Campus " & sYear & ".xlsx")

    Dim sht As Worksheet
    Dim fnd As Variant
    Dim rplc As Variant

    fnd = "\TBD\"
    rplc = "\" & sYear & "\"

    For Each sht In ActiveWorkbook.Worksheets
      sht.Cells.Replace what:=fnd, Replacement:=rplc, _
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
        SearchFormat:=False, ReplaceFormat:=False
    Next sht

    ActiveWorkbook.Close SaveChanges:=True
    
    ThisWorkbook.Activate
    
Release:

    Worksheets("Home").txtYear.Value = ""

End Sub

I get a runtime error at this line:
If Dir(sDC01 & sYear, vbDirectory) = "" Then


The code has to be able to run for whomever opens the file in the company, so I cannot use a local directory to save.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Cannot Find Correct O365 SharePoint Path For Excel VBA
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,677
Members
449,092
Latest member
tayo4dgacorbanget

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