Hi. I am new at all this, and have had tons of help with the below macro, but now we're needing someone to maybe review it and see where we've gone wrong. I am fully lost at this point, so any help would be greatly appreciated.
The left table is partial from the workbook that this macro will be saved to.
The right table is in another workbook called directory.xls (used for reference by the macro).
I’ve entered the variables used in the code to the tables for reference.
This Sheet
B.............D
pnum........qty
Directory.xls
A..........B........C
dnum.....dfol.....file
What we’re trying to do with the macro is :-
1. look at “pnum”, if this cell is blank then the macro ends.
2. If “pnum” contains an entry then it looks to “qty”, if this is blank or 0 then it looks at pnum in the next row.
3. When it finds “qty” greater than 0 it needs to search “dnum” in the directory to find an exact match for “pnum” exiting if no match is found. (thinking message and back to next row in #2 instead).
4. It then needs to take the text of “dfol” in the same row and pass that to the next part of the code as the source directory for the file copy, with “file” being the filename to copy.
5. After this is done it then needs to return to the top of the loop and look at the next entry in #2
Sub CopyFile()
Dim fso
Dim file As String, sfol As String, dfol As String
Dim pnum As String, qty As Integer, dnum As String
Dim x As Integer, y As Integer
x = 2
qty = ActiveSheet.Range("D" & x).Value
pnum = ActiveSheet.Range("B" & x).Text
If pnum = "" Then MsgBox "No more files to copy"
Exit Sub
If qty > 0 Then GoTo Line1 Else x = x + 1
Line1:
y = 1
dnum = Range("T:\DRAFTING RESOURCES\DATA SHEETS\[directory.xls]Sheet1!A" & y).Text
If dnum = "" Then MsgBox pnum & "Not found in directory": Exit Sub Else If dnum = pnum Then GoTo Line2 Else y = y + 1
Line2:
sfol = Range("T:\DRAFTING RESOURCES\DATA SHEETS\[directory.xls]Sheet1!B" & y).Text
file = Range("T:\DRAFTING RESOURCES\DATA SHEETS\[directory.xls]Sheet1!C" & y).Text
dfol = ActiveWorkbook.Path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(sfol & file) Then
MsgBox sfol & file & " does not exist!", vbExclamation, "Source File Missing"
ElseIf Not fso.FileExists(dfol & file) Then
fso.CopyFile (sfol & file), dfol, True
Else
MsgBox dfol & file & " already exists!", vbExclamation, "Destination File Exists"
End If
x = x + 1
Set y = 1
End Sub
Bookmarks