简体   繁体   中英

Pull latest workbook copy selected workbook and paste in master workbook

I am trying to look in a folder to pull the latest workbook by date, open the workbook up as my src data, copy the selected worksheet and data from src and then paste to my master workbook. Finally closing the src workbook without saving any change. I'm having issues on where I should place my file paths and filenames.

Function NewestFileName(ByVal path As String, ByVal FileTemplate As String) As String

Dim FileDateCrnt As Date
Dim FileDateNewest As Date
Dim FileNameCrnt As String
Dim FileNameNewest As String

If Right("G:\AOC\GROUPS1\SAC\TEST", 1) <> "\" Then
path = "G:\AOC\GROUPS1\SAC\TEST" & "\"
End If

FileNameCrnt = Dir$("G:\AOC\GROUPS1\SAC\TEST" & Book1.xlsx)
If FileNameCrnt = "Book1.xlsx" Then
NewestFileName = "Book2.xlsx"
Exit Function
End If

FileNameNewest = FileNameCrnt
FileDateNewest = FileDateTime("G:\AOC\GROUPS1\SAC\TEST" & FileNameCrnt)
Do While True
FileNameCrnt = Dir$
If FileNameCrnt = "" Then Exit Do
FileDateCrnt = FileDateTime(path & FileNameCrnt)
If FileDateCrnt > FileDateNewest Then
  FileNameNewest = FileNameCrnt
  FileDateNewest = FileDateCrnt
End If
Loop

NewestFileName = FileNameNewest

Call ReadDataFromCloseFile

End Function

Sub ReadDataFromCloseFile()
On Error GoTo ErrHandler
Application.ScreenUpdating = False

Dim src As Workbook

Set src = Workbook.Open("G:\AOC\GROUPS1\SAC\TEST.xlsx", True, True)

Dim iTotalRows As Integer
iTotalRows = src.Worksheets("sheet1").Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)

Dim iCnt As Integer
For iCnt = 1 To iTotalRows
Worksheets("sheet1").Range("B" & iCnt).Formula = src.Worksheets("sheet1").Range("B" & iCnt).Formula
Next iCnt

src.Close False
Set scr = Nothing

ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

First things first:

If you have a question or encounter an error, then spell it out. It's hard to find out where your error is, without knowing on which line it occurs.

Your function in a whole doesn't make that much sense. For taking a good look at it, commenting would've been very helpful.

Let's go through your code step by step:

If Right("G:\AOC\GROUPS1\SAC\TEST", 1) <> "\" Then
path = "G:\AOC\GROUPS1\SAC\TEST" & "\"
End If

This if-condition will always trigger, because the String you put in there, is always the same and it'll always miss the "\\". So if your path doesn't change then you can change that to path = "G:\\AOC\\GROUPS1\\SAC\\TEST\\"


FileNameCrnt = Dir$("G:\AOC\GROUPS1\SAC\TEST" & Book1.xlsx)
If FileNameCrnt = "Book1.xlsx" Then
NewestFileName = "Book2.xlsx"
Exit Function
End If

I'm not sure what you are trying to do here. You are setting FileNameCrnt to a string in the first line (you are missing the "\\" btw). I guess "Book1.xlsx" is the real name of your workbook, so your String should look either like this: "G:\\AOC\\GROUPS1\\SAC\\TEST\\Book1.xlsx" or you could do something like this

fileName = "Book1.xlsx"
FileNameCrnt = Dir$("G:\AOC\GROUPS1\SAC\TEST" & fileName )

Next: You would(!) always exit the function there, if the line above would work. You set FilenameCrnt to Book1.xlsx, then check it via an if-clause, the check will always return true , afterwards you'd always exit.


I get the idea of your loop, but it too is broken. Start by changing this: If FileNameCrnt = "" Then Exit Do to something else. Your variable will never be empty so your loop will always cause a runtime error. Start changing the first parts of your function and get to that later. I think you will have a better idea how all this should work. And it's always better to try solving some things by yourself. ;)

EDIT:

It's always helpful to make a flow chart on how your program should run. Something like:

  • Get my current filename
  • Get date of my current file
  • Check if there is a newer file (a file with a higher date than my old date)
  • Get dates of all files (loop through all files)
  • GET highest date
  • Compare highest date to date of my current file
  • if there is a file with a higher date, update current filename to filename with higher date

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM