英文:
clear contents before paste
问题
I have a workbook "DATABASE" with 5 sheets, I am trying to let my code copy contents of all 4 sheets and paste it to one master sheet "ARCHIVE" to be all together compiled.
我有一个名为“DATABASE”的工作簿,其中包含5个工作表,我试图让我的代码复制所有4个工作表的内容,然后粘贴到一个主工作表“ARCHIVE”中,以便将它们全部编译在一起。
I want each time the code runs, to clear contents in ARCHIVE and then paste copied values from other sheets. So that there will be no duplication every time is runs.
我希望每次运行代码时,都清除ARCHIVE中的内容,然后粘贴从其他工作表复制的值,以确保每次运行时都没有重复。
the code works fine before the clear thing, but when I add activesheets.cells.clearcontents
or sheets("ARCHIVE").cells.clearcontents
after the sheets("ARCHIVE").activate
it doesn't work.
在清除操作之前,该代码运行良好,但是当我在sheets("ARCHIVE").activate
之后添加activesheets.cells.clearcontents
或sheets("ARCHIVE").cells.clearcontents
时,它不起作用。
can someone help me where exactly should I put the clear contents code for ARCHIVE sheet before pasting? and if I should declare something before?
有人可以帮助我确定在粘贴之前应该将清除内容的代码放在ARCHIVE工作表的哪个位置吗?以及是否需要在之前声明一些内容?
I have put here the code while its working properly without the clear thing:
我在这里放置了代码,它在没有清除操作的情况下正常工作:
Sub CopyToMaster()
ShtCount = ActiveWorkbook.Sheets.Count
For I = 2 To ShtCount
Worksheets(I).Activate
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("a2:N" & LastRow).Select
Selection.Copy
Sheets("ARCHIVE").Activate
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Select
'Required after the first paste to shift the active cell down one
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(0, 0).Select
Selection.PasteSpecial
ActiveWorkbook.Save
Next I
End Sub
Sub tensecondstimer()
Application.OnTime Now + TimeValue("00:00:10"), "CopyToMaster"
End Sub
请让我知道如果需要更多帮助。
英文:
I have a workbook "DATABASE" with 5 sheets, I am trying to let my code copy contents of all 4 sheets and paste it to one master sheet "ARCHIVE" to be all together compiled.
I want each time the code runs, to clear contents in ARCHIVE and then paste copied values from other sheets. So that there will be no duplication every time is runs.
the code works fine before the clear thing, but when I add activesheets.cells.clearcontents
or sheets("ARCHIVE").cells.clearcontents
after the sheets("ARCHIVE").activate
it doesn't work.
can someone help me where exactly should I put the clear contents code for ARCHIVE sheet before pasting? and if I should declare something before?
I have put here the code while its working properly without the clear thing:
Sub CopyToMaster()
ShtCount = ActiveWorkbook.Sheets.Count
For I = 2 To ShtCount
Worksheets(I).Activate
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Range("a2:N" & LastRow).Select
Selection.Copy
Sheets("ARCHIVE").Activate
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Select
'Required after first paste to shift active cell down one
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(0, 0).Select
Selection.PasteSpecial
ActiveWorkbook.Save
Next I
End Sub
Sub tensecondstimer()
Application.OnTime Now + TimeValue("00:00:10"), "CopyToMaster"
End Sub
答案1
得分: 3
尝试
尝试
Sub 复制到主表()
Dim wb As Workbook
Dim ws As Worksheet
Dim archiveSheet As Worksheet
Dim lastRow As Long, archiveLastRow As Long, ShtCount As Long, i As Long
Set wb = ActiveWorkbook
Set archiveSheet = wb.Sheets("归档")
ShtCount = wb.Sheets.Count
For i = 2 To ShtCount
If i = 2 Then archiveSheet.Cells.ClearContents
lastRow = Worksheets(i).Cells(Worksheets(i).Rows.Count, "A").End(xlUp).Row
archiveLastRow = archiveSheet.Cells(archiveSheet.Rows.Count, "A").End(xlUp).Row
Worksheets(i).Range("A2:N" & lastRow).Copy
archiveSheet.Cells(archiveLastRow + 1, "A").PasteSpecial Paste:=xlPasteValues
Next i
wb.Save
Set wb = Nothing
Set ws = Nothing
Set archiveSheet = Nothing
十秒定时器
End Sub
Sub 十秒定时器()
Application.OnTime Now + TimeValue("00:00:10"), "复制到主表"
End Sub
英文:
try
Sub CopyToMaster()
Dim wb As Workbook
Dim ws As Worksheet
Dim archiveSheet As Worksheet
Dim lastRow As Long, archiveLastRow As Long, ShtCount As Long, i As Long
Set wb = ActiveWorkbook
Set archiveSheet = wb.Sheets("ARCHIVE")
ShtCount = wb.Sheets.Count
For i = 2 To ShtCount
If i = 2 Then archiveSheet.Cells.ClearContents
lastRow = Worksheets(i).Cells(Worksheets(i).Rows.Count, "A").End(xlUp).Row
archiveLastRow = archiveSheet.Cells(archiveSheet.Rows.Count, "A").End(xlUp).Row
Worksheets(i).Range("A2:N" & lastRow).Copy
archiveSheet.Cells(archiveLastRow + 1, "A").PasteSpecial Paste:=xlPasteValues
Next i
wb.Save
Set wb = Nothing
Set ws = Nothing
Set archiveSheet = Nothing
tensecondstimer
End Sub
Sub tensecondstimer()
Application.OnTime Now + TimeValue("00:00:10"), "CopyToMaster"
End Sub
答案2
得分: 3
复制到主工作表
<!-- 语言: lang-vb -->
选项 明确
子 复制到主工作表()
Dim wb As Workbook: Set wb = ThisWorkbook ' 包含此代码的工作簿
Dim dws As Worksheet: Set dws = wb.Sheets("Archive")
Dim dfCell As Range
用 dws.UsedRange
在错误时继续 ' 防止没有数据时出错
调整大小(.Rows.Count - 1).Offset(1).Clear ' 除标题外的所有内容
在错误时转到 0
设置 dfCell = .Cells(1).Offset(1) ' 第一个目标单元格(“A2”)
结束用
Dim sws As Worksheet, srg As Range
对于每个 sws 在 wb.Worksheets 中
如果 不是 sws 是 dws 然后 ' 排除目标工作表
用 sws.UsedRange
在错误时继续 ' 防止没有数据时出错
设置 srg = .Resize(.Rows.Count - 1).Offset(1)
在错误时转到 0
结束用
如果 不是 srg 是 无 则
srg.Copy dfCell
设置 dfCell = dfCell.Offset(srg.Rows.Count) ' 下一个第一个单元格
设置 srg = 无 ' 为下一次迭代重置
结束 如果
结束 如果
下一个 sws
wb.Save
结束 子
英文:
Copy to Master Worksheet
<!-- language: lang-vb -->
Option Explicit
Sub CopyToMaster()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim dws As Worksheet: Set dws = wb.Sheets("Archive")
Dim dfCell As Range
With dws.UsedRange
On Error Resume Next ' prevent error if no data
.Resize(.Rows.Count - 1).Offset(1).Clear ' all except headers
On Error GoTo 0
Set dfCell = .Cells(1).Offset(1) ' first destination cell ("A2")
End With
Dim sws As Worksheet, srg As Range
For Each sws In wb.Worksheets
If Not sws Is dws Then ' exclude destination worksheet
With sws.UsedRange
On Error Resume Next ' prevent error if no data
Set srg = .Resize(.Rows.Count - 1).Offset(1)
On Error GoTo 0
End With
If Not srg Is Nothing Then
srg.Copy dfCell
Set dfCell = dfCell.Offset(srg.Rows.Count) ' next first cell
Set srg = Nothing ' reset for the next iteration
End If
End If
Next sws
wb.Save
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论