I want to copy files to a shared OneDrive folder using VBA code

uc bucuk

New Member
Joined
Apr 21, 2022
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I am able to create a folder in a shared OneDrive folder using VBA code assigned to a button. However, if any of the shared individuals try to create a folder using the same button, the VBA code throws an error




VBA Code:
Sub CikanA1()
    Dim kaynakPath As String
    Dim hedefPath As String
    Dim yeniKlasorAdi As String
    
   
    If Range("C2").Interior.Color = RGB(255, 255, 0) Then
     
        kaynakPath = "C:\Users\ucbucuk\OneDrive - Astic\Masaüstü\Alp_QR\A1"
        
     
        hedefPath = "C:\Users\ucbucuk\OneDrive - Astic\Masaüstü\Alp_QR\___ÇIKAN İMALATLAR\"
        
        
        yeniKlasorAdi = Format(Date, "yyyy-mm-dd") & "-A1"
        
      
        On Error Resume Next
        MkDir hedefPath & yeniKlasorAdi
        On Error GoTo 0
        
      
        If Dir(hedefPath & yeniKlasorAdi, vbDirectory) <> "" Then
         
            Dim dosyaAdi As String
            dosyaAdi = Dir(kaynakPath & "\*.*", vbDirectory)
            
            Do While dosyaAdi <> ""
                If dosyaAdi <> "." And dosyaAdi <> ".." Then
                    FileCopy kaynakPath & "\" & dosyaAdi, hedefPath & yeniKlasorAdi & "\" & dosyaAdi
                End If
                dosyaAdi = Dir
            Loop
            
            MsgBox "Dosya kopyalandı.", vbInformation
        Else
            MsgBox "Hedef klasör oluşturulamadı.", vbCritical
        End If
    End If
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this, using FileSystemObject instead of native VBA file functions.
VBA Code:
Sub CikanA1()
    Dim kaynakPath As String
    Dim hedefPath As String
    Dim yeniKlasorAdi As String
    Dim FSO As Object 'Scripting.FileSystemObject
    Dim FSfile As Object 'Scripting.File
    
    Set FSO = CreateObject("Scripting.FileSystemObject") 'New Scripting.FileSystemObject
    
    If Range("C2").Interior.Color = RGB(255, 255, 0) Then
     
        kaynakPath = "C:\Users\ucbucuk\OneDrive - Astic\Masaüstü\Alp_QR\A1"
     
        hedefPath = "C:\Users\ucbucuk\OneDrive - Astic\Masaüstü\Alp_QR\___ÇIKAN IMALATLAR\"
        
        yeniKlasorAdi = Format(Date, "yyyy-mm-dd") & "-A1"
      
        If Not FSO.FolderExists(hedefPath & yeniKlasorAdi) Then FSO.CreateFolder hedefPath & yeniKlasorAdi
        
        For Each FSfile In FSO.GetFolder(kaynakPath).Files
            FSfile.Copy hedefPath & yeniKlasorAdi & "\" & FSfile.Name
        Next
        MsgBox "Dosya kopyalandi.", vbInformation
    
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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