Vba rename query

Pobek

Board Regular
Joined
Jul 7, 2015
Messages
99
I have a two texts files say text1.txt and text2.txt, I am trying to copy them over to another folder and rename them to (rename1 and rename2). The new names are in a particular column. I have written the code below but it is giving me a runtime error saying the source file cannot be found even though the link in the macro actually does open the file. This is the code below, can someone help please?

VBA Code:
Sub CopyFolderContents()
Dim fso As Object
Dim sourceFolder1, sourceFolder2, sourceFile1, sourceFile2 As String
Dim destinationFolder1, destinationFolder2, destParentFolder, destinationFile1, destinationFile2 As String
Dim i, j As Variant


For j = 1 To 5
    ' Initialize the FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    'Set the source and destination folder paths (change as needed)
    destParentFolder = "C:\XTemp" & "\" & Range("C47").Value2
    sourceFile1 = Cells(52 + j, 3).Value2 'Range("ParentOUT").Value2 & "\" & 203 ' "C:\Path\To\Source\Folder\"
    sourceFile2 = Cells(57 + j, 3).Value2 'Range("ParentOUT").Value2 & "\" & 402 ' "C:\Path\To\Source\Folder\"


    destinationFolder1 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 203
    destinationFolder2 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 402
    destinationFile1 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 203 & "\" & Cells(52 + j, 2).Value2 & "\" & ".txt"
    destinationFile2 = "C:\XTemp" & "\" & Range("C47").Value2 & "\" & 402 & "\" & Cells(57 + j, 2).Value2 & "\" & ".Am1"
  
    strFolderExists = Dir(destParentFolder, vbDirectory)
    If strFolderExists = "" Then MkDir Path:=destParentFolder
   
    strFolderExists = Dir(destinationFolder1, vbDirectory)
    If strFolderExists = "" Then MkDir Path:=destinationFolder1
   
    strFolderExists = Dir(destinationFolder2, vbDirectory)
    If strFolderExists = "" Then MkDir Path:=destinationFolder2

   fso.CopyFile sourceFile1, destinationFile1
    'fso.CopyFile Chr(34) & sourceFile1 & Chr(34), Chr(34) & destinationFile1 & Chr(34)
    fso.CopyFile sourceFile2, destinationFile2
    ' Clean up
    Set fso = Nothing
Next j

'MsgBox "All contents have been copied from " & sourceFolder & " to " & destinationFolder, vbInformation
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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