FSO question - copy files

ktab

Well-known Member
Joined
Apr 21, 2005
Messages
1,297
Hello,

I have the following problem with the code:

Dim fsoObj As Scripting.FileSystemObject
Const stPathFiles As String = ???????
Const stTarget As String = ????????
Set fsoObj = New Scripting.FileSystemObject
With fsoObj
If Not .FolderExists(stTarget) Then .CreateFolder (stTarget)
End With
fsoObj.CopyFile Source:=stPathFiles, Destination:=stTarget, OverWriteFiles:=True
Set fsoObj = Nothing

Where the bold ?? are how can i give as string a range?
I want to handle the code from userform so in case that i want to change path, just change, let's say sheet1.range("a1").value (e.g. c:\program files\kostas\)in a textbox and assign new value without having to change code all the time.
(if Const stPathFiles As String = sheet1.range("a1").value )

I hope my question is clear enough (i doubt)

Thank you
Kostas
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Kostas

You won't be able to use constants for your ranges.

Why not just use regular string variables?
 
Upvote 0
Or refer to the cells directly in your code...

<table border="1" bgcolor="White"><caption ALIGN=left><font size="2" face=Courier New>Example VBA Code:</FONT></caption><tr><td><font size="2" face=Courier New>  <font color="#0000A0">Dim</font> fsoObj <font color="#0000A0">As</font> Scripting.FileSystemObject
  <font color="#008000">'Const stPathFiles As String = ???????</font>
  <font color="#008000">'Const stTarget As String = ????????</font>
  <font color="#0000A0">Set</font> fsoObj = <font color="#0000A0">New</font> Scripting.FileSystemObject
  <font color="#0000A0">With</font> fsoObj
  <font color="#0000A0">If</font> <font color="#0000A0">Not</font> .FolderExists(stTarget) <font color="#0000A0">Then</font> .CreateFolder (stTarget)
  <font color="#0000A0">End</font> <font color="#0000A0">With</font>
  fsoObj.CopyFile Source:=Sheet1.Range("a1").Text, _
       Destination:=Sheet1.Range("a2").Text, OverWriteFiles:=True
  <font color="#0000A0">Set</font> fsoObj = <font color="#0000A0">Nothing</font>
</FONT></td></tr></table>

Tom
 
Upvote 0
Thank you Norie, that did he job.

Just to help others that might need the code, you got to relace also at:
If Not .FolderExists(stTarget) Then .CreateFolder (stTarget)
where stTarget is the desired cell range
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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