Assuming your worksheet looks like this:-
<TABLE style="WIDTH: 170pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=227><COLGROUP><COL style="WIDTH: 29pt; mso-width-source: userset; mso-width-alt: 1426" width=39><COL style="WIDTH: 141pt; mso-width-source: userset; mso-width-alt: 6875" width=188><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 29pt; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" id=td_post_2798760 height=20 width=39></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #dbe5f1; WIDTH: 141pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl64 width=188>
A
</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #dbe5f1; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl64 height=20 align=right>
1
</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63>
c:\folder1\</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #dbe5f1; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl64 height=20 align=right>
2
</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63>
c:\folder2\temp.txt</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #dbe5f1; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl64 height=20 align=right>
3
</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl63>
temp99.txt</TD></TR></TBODY></TABLE>
This code should do the job:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Sub CopyRename()[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] Dim sFolder As String
Dim sFilename As String
Dim sNewFilename As String
sFolder = ActiveSheet.Range("A1").Value
If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
If Dir(sFolder, vbDirectory) = "" Then MkDir sFolder
sFilename = ActiveSheet.Range("A2").Value
If Dir(sFilename) = "" Then
MsgBox sFilename & " does not exist!", vbOKOnly + vbExclamation
Exit Sub
End If
sNewFilename = ActiveSheet.Range("A3").Value
If Dir(sFolder & sNewFilename) <> "" Then
MsgBox sFolder & sNewFilename & " already exists!", vbOKOnly + vbExclamation
Exit Sub
End If
FileCopy sFilename, sFolder & sNewFilename
If Dir(sFolder & sNewFilename) = "" Then
MsgBox sFilename & " copy failed!", vbOKOnly + vbExclamation
Exit Sub
End If
MsgBox sFilename & " copied to " & sFolder & sNewFilename, vbOKOnly + vbInformation
[/FONT]
[FONT=Fixedsys]End Sub
[/FONT]
To install this code, in new worksheet, press
Alt-F11 to open Microsoft Visual Basic, press
Ctrl-R to view the Project Explorer, then
go
Insert >
Module. A new 'standard' code module will appear under
Modules, probably called
Module1. You might need to click the + symbol against
Modules to expand the group.
Double-click the name of this new module to open it. Remove any code you find in the code window (probably only the words
Option Explicit, if anything) and paste my code in its place - everything from
Option Explicit down to and including
End Sub.
Return to your worksheet. Make sure you have a new folder name in A1, an existing filename in A2 and a the name you want for the copied file in A3. (You can change all these once you've got everything working okay.) The code tries to detect simple error conditions like no backslash on the end of the folder name, the source file not existing, the target file already existing, etc.
Finally click
Developer >
Macros, select the macro called
CopyRename from the dialog box and click
Run. Check that the new folder has been created and that the correct file has been copied with the correct new name.
If you have any problems, this code comes with a 24-hour warranty!