宏:打开文件夹中的最新文件并复制粘贴到最后一个单元格下方
Macro : Open Latest File in Folder and Copy Paste Below Last Cell
如何在 Excel VBA 中合并这两部分代码?
Part 1:
Sub Copypastelastrow()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim LMD As Variant
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
MyPath = "C:\Users\andrew\Desktop\newdoc"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
我不明白如何将打开的工作簿设置为活动工作簿并复制粘贴到最后一行下方。我认为错误在第 2 部分:对象变量或块变量未设置!
第 2 部分:
Set wsCopy = Workbooks("Workbooks.Open MyPath & LatestFile").Worksheets("sheet1")
Set wsDest = Workbooks("Workbook2").Worksheets("sheet1")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
wsCopy.Range("S3:T" & lCopyLastRow).Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
引用工作簿
当你打开一个工作簿时,它变成 ActiveWorkbook
:
Workbooks.Open MyPath & LatestFile
Set wsCopy = ActiveWorkbook.Worksheets("Sheet1")
或一行:
Set wsCopy = Workbooks.Open(MyPath & LastestFile).Worksheets("Sheet1")
稍后要关闭工作簿,您将使用:
wsCopy.Parent.Close SaveChanges:=False ' usually because it's only read from.
使用变量:
Workbooks.Open MyPath & LatestFile
Dim wb As Workbook: Set wb = ActiveWorkbook
Set wsCopy = wb.Worksheets("Sheet1")
或:
Dim wb As Workbook
Set wb = Workbooks.Open(MyPath & LatestFile)
Set wsCopy = wb.Worksheets("Sheet1")
稍后要关闭工作簿,您将使用:
wsCopy.Parent.Close SaveChanges:=False ' usually because it's only read from.
或
wb.Close SaveChanges:=False ' usually because it's only read from.
如何在 Excel VBA 中合并这两部分代码?
Part 1:
Sub Copypastelastrow()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim LMD As Variant
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
MyPath = "C:\Users\andrew\Desktop\newdoc"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
我不明白如何将打开的工作簿设置为活动工作簿并复制粘贴到最后一行下方。我认为错误在第 2 部分:对象变量或块变量未设置!
第 2 部分:
Set wsCopy = Workbooks("Workbooks.Open MyPath & LatestFile").Worksheets("sheet1")
Set wsDest = Workbooks("Workbook2").Worksheets("sheet1")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
wsCopy.Range("S3:T" & lCopyLastRow).Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
引用工作簿
当你打开一个工作簿时,它变成
ActiveWorkbook
:Workbooks.Open MyPath & LatestFile Set wsCopy = ActiveWorkbook.Worksheets("Sheet1")
或一行:
Set wsCopy = Workbooks.Open(MyPath & LastestFile).Worksheets("Sheet1")
稍后要关闭工作簿,您将使用:
wsCopy.Parent.Close SaveChanges:=False ' usually because it's only read from.
使用变量:
Workbooks.Open MyPath & LatestFile Dim wb As Workbook: Set wb = ActiveWorkbook Set wsCopy = wb.Worksheets("Sheet1")
或:
Dim wb As Workbook Set wb = Workbooks.Open(MyPath & LatestFile) Set wsCopy = wb.Worksheets("Sheet1")
稍后要关闭工作簿,您将使用:
wsCopy.Parent.Close SaveChanges:=False ' usually because it's only read from.
或
wb.Close SaveChanges:=False ' usually because it's only read from.