copy paste a file if its not present & if its present then do nothing

agog12

Board Regular
Joined
Jan 23, 2018
Messages
86
my file name is 4.xlsx
i have to copy this file and paste it to another path( if 4.xlsx file doesn't exist then copy paste it to C:\Users\WolfieeeStyle\Desktop/sholtan , but if file is there in the path C:\Users\WolfieeeStyle\Desktop/sholtan then do nothing)
i want to do this by vba so plz have a look sir and help me in solving this problem sir
file is located at C:\Users\WolfieeeStyle\Desktop/4.xlsx and we have to copy this file and paste it to C:\Users\WolfieeeStyle\Desktop/sholtan
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
5,249
Office Version
365
Platform
Windows
You should be able to make use of FileSystemObject for this

Below link is an excellent source for you to read and learn about it

 

agog12

Board Regular
Joined
Jan 23, 2018
Messages
86
Bro i saw the code that code is adavanced code and i dont want that i dont want any msgbox
plz help me in solving this problem
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
5,249
Office Version
365
Platform
Windows
You could try this method which works for me
- amend to match your folder structure

VBA Code:
Sub CopyMyFile()
    Const oldPath = "C:\TestFolder\SubFolder1\4.xlsx "
    Const newPath = "C:\TestFolder\SubFolder2\4.xlsx "
    If Dir(newPath) = "" Then FileCopy oldPath, newPath
End Sub
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
5,249
Office Version
365
Platform
Windows
Another method which also works

VBA Code:
Sub CopyMyFile2()
    Dim wb As Workbook
    Const oldPath = "C:\TestFolder\SubFolder1\4.xlsx "
    Const newPath = "C:\TestFolder\SubFolder2\4.xlsx "

    If Dir(newPath) = "" Then
        Set wb = Workbooks.Open(oldPath)
        wb.SaveAs (newPath)
        wb.Close False
    End If
End Sub
 

Forum statistics

Threads
1,089,220
Messages
5,406,925
Members
403,113
Latest member
ms_excel_recal_or_die

This Week's Hot Topics

Top