VBA Help to identify files with REV numbers (Excel data is without the REV)

dchaney

Well-known Member
Joined
Jun 4, 2008
Messages
732
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I have created a VBA macro that will move files from one folder to the next based on the name of the file and the parent file. What I am having issues with is the excel file has the drawing number of the file (1234567890), and the file was saved as the drawing number and a revision number (1234567890_REV000). The rev number can change from file to file and I am not sure how to get past that. Here is what I have currently.

Code:
Sub MoveFiles() 'Designed to move files in a folder based on the information in an excel file (BP R05 - L01 Material Certs)

Dim FromPath As String, ToPath As String, FolderName As String, FileName As String
Dim lastRow As Long
Dim FSO As Object

'Turn off Application Functions
    With Application
        .CutCopyMode = False
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
    
On Error Resume Next

Set FSO = CreateObject("scripting.filesystemobject")

'Select the proper from directory
Set DialogFolder = Application.FileDialog(msoFileDialogFolderPicker)
    DialogFolder.InitialFileName = ActiveWorkbook.Path
    If DialogFolder.Show = -1 Then
        FromPath = DialogFolder.SelectedItems(1)
    Else: Set DialogFolder = Nothing
    End If
    
Set DialogFolder = Nothing

lastRow = Range("A" & Rows.Count).End(xlUp).Row

i = 9

Do Until i > lastRow

FolderName = Cells(i, 2) & " - " & Cells(i, 6) & "\"
ToPath = FromPath & FolderName
FileName = Cells(i, 7) & "_REV" & "*.pdf"

If Cells(i, 1).Interior.ColorIndex = 15 Then
        ParentFolder = Cells(i, 2).Value & " - " & Cells(i, 6).Value & "\"
        FSO.MoveFile Source:=FromPath & FileName, Destination:=ToPath
    i = i + 1
ElseIf Cells(i, 1).Interior.ColorIndex <> 15 Then
        FSO.CopyFile Source:=FromPath & FileName, Destination:=FromPath & ParentFolder
    i = i + 1
End If

Loop

Set FSO = Nothing

Kill FromPath & "*.pdf"

MsgBox "All of your files have been moved"

'Turn on Application Functions
    With Application
        .CutCopyMode = False
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
    
End Sub

The issue part is FileName = Cells(i, 7) & "_REV" & "*.pdf" not sure how to get past this and have the code see 1234567890_REV000 as the 1234567890 number from my excel file.

Any help would be greatly appreciated, teaching myself VBA and it is a challenge to say the least
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I found errors in the code I pasted here (was a previous version) I had made changes to make the code work for me, if the rev version was 000, but if it is a REV 001 or other it will not work

Code:
Sub MoveFiles() 'Designed to move files in a folder based on the information in an excel file (BP R05 - L01 Material Certs)

Dim FromPath As String, ToPath As String, FolderName As String, FileName As String
Dim lastRow As Long
Dim FSO As Object

'Turn off Application Functions
    With Application
        .CutCopyMode = False
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
    
On Error Resume Next

Set FSO = CreateObject("scripting.filesystemobject")

'Select the proper from directory
Set DialogFolder = Application.FileDialog(msoFileDialogFolderPicker)
    DialogFolder.InitialFileName = ActiveWorkbook.Path
    If DialogFolder.Show = -1 Then
        FromPath = DialogFolder.SelectedItems(1) & "\"
    Else: Set DialogFolder = Nothing
    End If
    
Set DialogFolder = Nothing

lastRow = Range("A" & Rows.Count).End(xlUp).Row

i = 9

Do Until i > lastRow

FolderName = Cells(i, 2) & " - " & Cells(i, 6) & "\"
ToPath = FromPath & FolderName
FileName = Cells(i, 7) & "_REV000.pdf"

Cells(1, 15) = FromPath

If Cells(i, 1).Interior.ColorIndex = 15 Then
        ParentFolder = FromPath & Cells(i, 2) & " - " & Cells(i, 6) & "\"
        FSO.MoveFile Source:=FromPath & FileName, Destination:=ToPath
    i = i + 1
ElseIf Cells(i, 1).Interior.ColorIndex <> 15 Then
        FSO.CopyFile Source:=FromPath & FileName, Destination:=ParentFolder
    i = i + 1
End If

Loop

Set FSO = Nothing

Kill FromPath & "*.pdf"

MsgBox "All of your files have been moved"

'Turn on Application Functions
    With Application
        .CutCopyMode = False
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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