I have an Excel file where columns (icol) each cells contains a path of some files like this :
column A column B column c
P:\Desktop\Source\Test1-folder\file1.txt empty column P:\Desktop\Source\Test1-folder\filetest.txt
P:\Desktop\Source\Test1-folder\file2.txt .....
and I need to loop through these cells to copy files from the cells into destination folder, but i couldn't succeed .Can anyone help how to do it?
Dim strSlash As String, destinationFolder As String
Dim lastcolumn As Long, icol As Long, lastLigne As Long
Dim rngCell As Range, rngFiles As Range
Dim FSO As New FileSystemObject
destinationFolder = "P:\Desktop\folderdestination"
Dim maListe As Object
Dim workboo As Workbook
Dim worksh As Worksheet
Set workboo = Workbooks.Open(P:\Desktop\Source\excelfile.xlsx)
Set worksh = workboo.Worksheets("path_files")
lastcolumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
If Dir(destinationFolder, 16) = "" Then MkDir (destinationFolder)
For icol = 1 To lastcolumn Step 2
lastLigne = Cells(Rows.Count, icol).End(xlUp).Row
Set rngFiles = Cells(1, icol).Resize(lastLigne)
For Each rngCell In rngFiles.Cells
If Dir(rngCell.Value) <> "" Then
strFile = Right(rngCell.Value, Len(rngCell.Value) - InStrRev(rngCell.Value, "\"))
If Dir(destinationFolder & "\" & Left(strFile, 5) , 16) = "" Then
FSO.CopyFile rngCell.Value, destinationFolder & "\" & Left(strFile, 5)
End If
End If
Next rngCell
Next icol
end sub
Next
for the firstFor
loop... Have you definitely posted ALL the code? It seems like there's a fair bit missing... – Davedestination folder
todestinationFolder
, variables can't have spaces in them. Then replace any instance ofworksh
withws
as that's what you declared and set to be the worksheet. I still don't see dercol (in the lineFor k = 1 To dercol
) defined anywhere so that's going to initialise to zero and prevent the loop executing. You are still missing aNext
as you have twoFor
loops and each needs its ownNext
... – Dave