嗨,伙计们,我在为Udemy做一门课程,很不幸的是,这位讲师的反应并不是最好的。
我有一本名为QuarterlyReport的工作簿和5张工作表。
我的代码格式化工作表1-4,然后复制,将信息粘贴到最后未使用行的年度报告中。由于某种原因,代码只粘贴South。我的目标是复制每一张1-4页,并将其粘贴到第五张“年度报告”上。。
Public Sub Finalreportloop()
Dim i As Integer
i = 1
Do While i <= Worksheets.Count - 1
Worksheets(i).Select
AddHeaders
FormatData
AutoSum
' copy the current data
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Copy
' select the final report WS'
Worksheets("yearly report").Select
'find the empty cells
LastRow = Sheets(i).Range("A" & Sheets(i).Rows.Count).End(xlUp).Row
'paste the new data in
ActiveSheet.Paste
i = i + 1
Loop
End Sub
Addheaders、FormatData和AutoSum引用了我创建的其他模块。谢谢大家!
发布于 2019-04-09 04:34:24
代码确定要从其中复制的工作表的最后一行,但对该信息不做任何操作。相反,它只是粘贴到活动表中,并覆盖在最后一个循环实例中提交的数据。因此,它似乎只是在复制/粘贴最后一个数据集。
您需要在年度工作表中找到最后一行,然后粘贴下面的数据。
发布于 2019-04-09 06:33:34
您可以尝试以下方法之一:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim wsLastRow As Long, wsLastColumn As Long, yrLastRow As Long
Dim rngCopy As Range
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "YEARLY REPORT" Then
With ws
'Method 1 - You can siply used range
' .UsedRange.Copy
'Method 2 - You can calculate LastColumn & LastRow and create the range
wsLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Find last row of column A.
wsLastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column '<- Find the last column of row 1.
Set rngCopy = .Range(Cells(1, 1), Cells(wsLastRow, wsLastColumn)) '<- Create the range to be copy.
rngCopy.Copy
End With
With ThisWorkbook.Worksheets("YEARLY REPORT")
yrLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Find last row of column A.
.Range("A" & yrLastRow + 1).PasteSpecial xlPasteValues
End With
End If
Application.CutCopyMode = False
Next ws
End Sub
https://stackoverflow.com/questions/55585098
复制相似问题