英文:
Copy data from a monthly folder into a sheet (without opening and closing workbooks)
问题
以下是您提供的VBA代码的翻译部分:
Sub getlatestfilename()
Dim F As String, folder As String, currentyear As Integer, currentmonth As String, foldername As String, myfile As String
Dim LatestFile As String, filetoopen As String
Dim LatestDate As Date
Dim LMD As Date
Dim LR As Long
Dim datawb As Workbook, thiswb As Workbook, ws As Worksheet
' 一旦确保它可以运行,请取消下面的注释
Application.ScreenUpdating = False
Set thiswb = ActiveWorkbook
currentyear = Year(Date)
currentmonth = Format(Month(Date), "00")
folder = "K:\Finance\Protected Funding Sheets\Barclays cash funding\Daily Funding Calculation\" & currentyear '& "\"
F = Dir(folder & "\*", vbDirectory)
Do While F <> ""
If InStr(F, currentmonth) > 0 Then
foldername = F
'Debug.Print foldername
folder = folder & "\" & foldername & "\"
Exit Do
End If
F = Dir
Loop
' 检查是否找到了月份文件夹
If F = "" Then
MsgBox "未找到 " & currentmonth & " 月份文件夹..... ", vbExclamation
Exit Sub
End If
'Debug.Print folder
' 确保路径以反斜杠结尾
If Right(folder, 1) <> "\" Then folder = folder & "\"
' 从文件夹中获取第一个Excel文件
myfile = Dir(folder & "*.xlsx", vbNormal)
' 如果没有找到文件,退出子程序
If Len(myfile) = 0 Then
MsgBox "未找到文件...", vbExclamation
Exit Sub
End If
' 遍历文件夹中的每个Excel文件
Do While Len(myfile) > 0
' 将当前文件的日期/时间分配给一个变量
LMD = FileDateTime(folder & myfile)
' 如果当前文件的日期/时间大于最新记录的日期,将其文件名和日期/时间分配给变量
If LMD > LatestDate Then
LatestFile = myfile
LatestDate = LMD
End If
' 获取文件夹中的下一个Excel文件
myfile = Dir
Loop
'Debug.Print LatestFile, LatestDate,
filetoopen = folder & LatestFile
'Debug.Print filetoopen
Set datawb = Workbooks.Open(filetoopen, Password:="barclays")
' 选择正确的工作表
' 将工作表名称更改为文件中使用的名称
' datawb.Sheets("sheetname").Activate
With datawb.Sheets("Journal")
date1 = .Range("B1")
date2 = .Range("C1")
bca = .Range("C16")
bcabs41 = .Range("H19")
bcabs42 = .Range("H20")
csh = .Range("K15")
cshbs42 = .Range("O15")
cshbs43 = .Range("O16")
cshbs432 = .Range("O18")
cshbs44 = .Range("O19")
' 添加其他所需的单元格
End With
datawb.Close savechanges:=False
Set ws = thiswb.Sheets("Postings")
ws.Activate
' 用于理解 LR = 最后一行
' 将数据变量添加到最后一行 + 1
With ws
LR = .Cells(Rows.Count, 1).End(xlUp).Row
' 添加保存的变量
.Cells(LR + 1, 1) = date1
.Cells(LR + 1, 2) = date2
.Cells(LR + 1, 3) = bca
.Cells(LR + 1, 4) = bcabs41
.Cells(LR + 1, 5) = bcabs42
.Cells(LR + 1, 6) = csh
.Cells(LR + 1, 7) = cshbs42
.Cells(LR + 1, 8) = cshbs43
.Cells(LR + 1, 9) = cshbs432
.Cells(LR + 1, 10) = cshbs44
' 添加其他所需的单元格
End With
Application.ScreenUpdating = True
End Sub
希望这对您有所帮助!如果您有任何其他问题,请随时提问。
英文:
I have a monthly journal I complete each month where I copy data from a daily workbook to another sheet.
I have code for the current day. It opens and closes the current day's sheet.
I would like to not open and close the file and also do a month's worth of data instead, so it takes all the data from all the sheets in that month's folder.
Sub getlatestfilename()
Dim F As String, folder As String, currentyear As Integer, currentmonth As String, foldername As String, myfile As String
Dim LatestFile As String, filetoopen As String
Dim LatestDate As Date
Dim LMD As Date
Dim LR As Long
Dim datawb As Workbook, thiswb As Workbook, ws As Worksheet
' uncomment below once happy it runs
Application.ScreenUpdating = False
Set thiswb = ActiveWorkbook
currentyear = Year(Date)
currentmonth = Format(Month(Date), "00")
folder = "K:\Finance\Protected Funding Sheets\Barclays cash funding\Daily Funding Calculation\" & currentyear '& "\"
F = Dir(folder & "\*", vbDirectory)
Do While F <> ""
If InStr(F, currentmonth) > 0 Then
foldername = F
'Debug.Print foldername
folder = folder & "\" & foldername & "\"
Exit Do
End If
F = Dir
Loop
' check the month folder has been found
If F = "" Then
MsgBox "No " & currentmonth & " folder found..... ", vbExclamation
Exit Sub
End If
'Debug.Print folder
'Make sure that the path ends in a backslash
If Right(folder, 1) <> "\" Then folder = folder & "\"
'Get the first Excel file from the folder
myfile = Dir(folder & "*.xlsx", vbNormal)
'If no files were found, exit the sub
If Len(myfile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(myfile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(folder & myfile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = myfile
LatestDate = LMD
End If
'Get the next Excel file from the folder
myfile = Dir
Loop
'Debug.Print LatestFile, LatestDate,
filetoopen = folder & LatestFile
'Debug.Print filetoopen
Set datawb = Workbooks.Open(filetoopen, Password:="barclays")
'select the correct sheet
'change sheetname to what is used in the file
'datawb.Sheets("sheetname").Activate
With datawb.Sheets("Journal")
date1 = .Range("B1")
date2 = .Range("C1")
bca = .Range("C16")
bcabs41 = .Range("H19")
bcabs42 = .Range("H20")
csh = .Range("K15")
cshbs42 = .Range("O15")
cshbs43 = .Range("O16")
cshbs432 = .Range("O18")
cshbs44 = .Range("O19")
'add the other required cells
End With
datawb.Close savechanges = False
Set ws = thiswb.Sheets("Postings")
ws.Activate
'For understanding LR = Last Row
'add variables data to the last row + 1
With ws
LR = .Cells(Rows.Count, 1).End(xlUp).Row
'add the saved variables
.Cells(LR + 1, 1) = date1
.Cells(LR + 1, 2) = date2
.Cells(LR + 1, 3) = bca
.Cells(LR + 1, 4) = bcabs41
.Cells(LR + 1, 5) = bcabs42
.Cells(LR + 1, 6) = csh
.Cells(LR + 1, 7) = cshbs42
.Cells(LR + 1, 8) = cshbs43
.Cells(LR + 1, 9) = cshbs432
.Cells(LR + 1, 10) = cshbs44
'add the other required cells
End With
Application.ScreenUpdating = True
End Sub
答案1
得分: 1
您已经有一个循环遍历文件夹中所有文件的代码部分:
'循环遍历文件夹中的每个Excel文件
Do While Len(myfile) > 0
...
Loop
不仅在此循环内分配LatestFile
,还将所有复制/粘贴活动放在此循环内。
英文:
You already have a section of code that loops through all files in the folder:
'Loop through each Excel file in the folder
Do While Len(myfile) > 0
...
Loop
Instead of just assigning the LatestFile
inside this loop, put all of your copy / paste activity inside this loop.
Sub getlatestfilename()
Dim F As String, folder As String, currentyear As Integer, currentmonth As String, foldername As String, myfile As String
Dim LatestFile As String, filetoopen As String
Dim LatestDate As Date
Dim LMD As Date
Dim LR As Long
Dim datawb As Workbook, thiswb As Workbook, ws As Worksheet
' uncomment below once happy it runs
Application.ScreenUpdating = False
Set thiswb = ActiveWorkbook
currentyear = Year(Date)
currentmonth = Format(Month(Date), "00")
folder = "K:\Finance\Protected Funding Sheets\Barclays cash funding\Daily Funding Calculation\" & currentyear '& "\"
F = Dir(folder & "\*", vbDirectory)
Do While F <> ""
If InStr(F, currentmonth) > 0 Then
foldername = F
'Debug.Print foldername
folder = folder & "\" & foldername & "\"
Exit Do
End If
F = Dir
Loop
' check the month folder has been found
If F = "" Then
MsgBox "No " & currentmonth & " folder found..... ", vbExclamation
Exit Sub
End If
'Debug.Print folder
'Make sure that the path ends in a backslash
If Right(folder, 1) <> "\" Then folder = folder & "\"
'Get the first Excel file from the folder
myfile = Dir(folder & "*.xlsx", vbNormal)
'If no files were found, exit the sub
If Len(myfile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(myfile) > 0
filetoopen = folder & myfile
'Debug.Print filetoopen
Set datawb = Workbooks.Open(filetoopen, Password:="barclays")
'select the correct sheet
'change sheetname to what is used in the file
'datawb.Sheets("sheetname").Activate
With datawb.Sheets("Journal")
date1 = .Range("B1")
date2 = .Range("C1")
bca = .Range("C16")
bcabs41 = .Range("H19")
bcabs42 = .Range("H20")
csh = .Range("K15")
cshbs42 = .Range("O15")
cshbs43 = .Range("O16")
cshbs432 = .Range("O18")
cshbs44 = .Range("O19")
'add the other required cells
End With
datawb.Close savechanges = False
Set ws = thiswb.Sheets("Postings")
ws.Activate
'For understanding LR = Last Row
'add variables data to the last row + 1
With ws
LR = .Cells(Rows.Count, 1).End(xlUp).Row
'add the saved variables
.Cells(LR + 1, 1) = date1
.Cells(LR + 1, 2) = date2
.Cells(LR + 1, 3) = bca
.Cells(LR + 1, 4) = bcabs41
.Cells(LR + 1, 5) = bcabs42
.Cells(LR + 1, 6) = csh
.Cells(LR + 1, 7) = cshbs42
.Cells(LR + 1, 8) = cshbs43
.Cells(LR + 1, 9) = cshbs432
.Cells(LR + 1, 10) = cshbs44
'add the other required cells
End With
'Get the next Excel file from the folder
myfile = Dir
Loop
Application.ScreenUpdating = True
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论