Copy file with VBA

Tofik

Board Regular
Joined
Feb 4, 2021
Messages
114
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi guys Hi guys, I need a VBA code which will be copy some files from Folder 1 to the Folder 2 . When I search in the internet i found some written solution but they are not exactly what I am looking for.
I want to copy not the all ( B4:B12) files it will be for example only B6, in the next search B3 and etc....
That's why I created a lot of buttons for this operation. ( See picture below )

Thanks .


TEST.xlsm
ABCDE
1FOLDER:C:\Folder 1
2FIND:08315-0001
3
41HS523217-21-PIP-ISO-08315-0001_1.pdf5/15/2021to Folder 2
5to Folder 2
6to Folder 2
7to Folder 2
8to Folder 2
9to Folder 2
10to Folder 2
11to Folder 2
12to Folder 2
13
14Copy button should work in next condition :
15B4 (file name which I found in C:\Folder 1 ) shold be copied from searched Folder 1 to the Folder 2
16B5 (file name which I found in C:\Folder 1 ) shold be copied from searched Folder 1 to the Folder 2
17B6 (file name which I found in C:\Folder 1) shold be copied from searched Folder 1 to the Folder 2
18B7 (file name which I found in C:\Folder 1) shold be copied from searched Folder 1 to the Folder 2
19B8 (file name which I found in C:\Folder 1) shold be copied from searched Folder 1 to the Folder 2
20B9 (file name which I found in C:\Folder 1) shold be copied from searched Folder 1 to the Folder 2
21
22C:\Folder 1
23C:\Folder 2
24
DRAWINGS ISO




1651129223317.png
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
something like this :

Code:
Public Sub Copy1FileInList()
Dim vSrcDir, vTargDir, vSrcFile, vTargFile, vVal
Dim r As Long

vSrcDir = FixDir(Range("B1").Value)

r = ActiveCell.Row
Range("B" & r).Select
vVal = ActiveCell.Value
vTargDir = FixDir(ActiveCell.Offset(0, 3).Value)
vSrcFile = vSrcDir & vVal
vTargFile = vTargDir & vVal
 
Copy1File vSrcFile, vTargDir
 
End Sub

Private Function FixDir(pvPath)
If pvPath = "" Then Exit Function
If Right(pvPath, 1) <> "\" Then pvPath = pvPath & "\"
FixDir = pvPath
End Function

Private Function Copy1File(ByVal pvSrc, ByVal pvTarg) As Boolean
Dim fso
On Error GoTo errMake
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile pvSrc, pvTarg
Copy1File = True
Set fso = Nothing
Exit Function
errMake:
'MsgBox Err.Description & vbCrLf & pvSrc, , "Copy1File(): " & Err
Set fso = Nothing
End Function
 
Last edited:
Upvote 0
Hi

ranman256, I have a question about the Folder 2.​

Where I should make some corrections about the Folder 2 in your code I can't find it?
Can you explain this think to me ?

About Folder 1 I understand in your code and it should be
vSrcDir = FixDir(Range("B1").Value) which = Folder 1



something like this :

Code:
Public Sub Copy1FileInList()
Dim vSrcDir, vTargDir, vSrcFile, vTargFile, vVal
Dim r As Long

vSrcDir = FixDir(Range("B1").Value)

r = ActiveCell.Row
Range("B" & r).Select
vVal = ActiveCell.Value
vTargDir = FixDir(ActiveCell.Offset(0, 3).Value)
vSrcFile = vSrcDir & vVal
vTargFile = vTargDir & vVal
 
Copy1File vSrcFile, vTargDir
 
End Sub

Private Function FixDir(pvPath)
If pvPath = "" Then Exit Function
If Right(pvPath, 1) <> "\" Then pvPath = pvPath & "\"
FixDir = pvPath
End Function

Private Function Copy1File(ByVal pvSrc, ByVal pvTarg) As Boolean
Dim fso
On Error GoTo errMake
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile pvSrc, pvTarg
Copy1File = True
Set fso = Nothing
Exit Function
errMake:
'MsgBox Err.Description & vbCrLf & pvSrc, , "Copy1File(): " & Err
Set fso = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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