Moving files Macro

ninjabik2001

New Member
Joined
Jun 27, 2019
Messages
3
Hi, I am still fairly new at coding. I have this macro (not mine, I didn't write it) that works perfectly but I don't want it to copy. I want it to cut and paste the files instead of copying. That way, they no longer exist in the folder from where I moved them from. I have tried to figure it out, but can' find the answer. Thanks ahead of time. The current working code is below.

Public Sub MoveFiles()
' Move any FolderA files (columnA) to dirs in ColumnB
'

Const colA = 1
Const colB = 2
Const colC = 3
Const FolderA = "S:\dept\THN\Dashboard - PCP Quality\2019" ' NOTE trailing backslash
Const srcSheet = "Source"

Dim xlS As Excel.Worksheet
Dim xlW As Excel.Workbook
Dim RN As Long ' row number
Dim fName As String
Dim fPath As String

' get ready

Set xlW = ActiveWorkbook
Set xlS = xlW.Sheets(srcSheet)

RN = 2
fName = Trim(xlS.Cells(RN, colA).Text)

' We'll run thru ColA until we hit a blank

On Error Resume Next ' expect problems if no target Dir

While fName <> ""

' if it hasn't aready been moved

If Trim(xlS.Cells(RN, colC).Text) = "" Then

' got one.
' Get the path. Ensure trailing backslash

fPath = Trim(xlS.Cells(RN, colB).Text)

If Right(fPath, 1) <> "" Then fPath = fPath & ""

' if the target already exists, nuke it.

If Dir(fPath & fName) <> "" Then Kill fPath & fName

' move it

FileCopy FolderA & fName, fPath & fName
DoEvents

' report it

If Err.Number <> 0 Then

xlS.Cells(RN, colC).Value = "Failed: Check target Dir"

Err.Clear

Else

xlS.Cells(RN, colC).Value = Now()

End If
End If

' ready for next one

RN = RN + 1
fName = Trim(xlS.Cells(RN, colA).Text)

Wend

MsgBox "Done it!!"

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If as you say, the code you posted "works perfectly", then all you should need to do is add a VBA "kill" statement after FileCopy to delete the source file after it is copied.

Code:
            ' move it
            FileCopy FolderA & fName, fPath & fName
[COLOR="#0000FF"]            Kill FolderA & fName[/COLOR]
            DoEvents
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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