copying contents from multiple sheets and paste to one sheet while clearing contents before.

whitebutterfly000

New Member
Joined
May 20, 2023
Messages
1
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello all,
Hope you can answer me I have been trying to add
(clearcontents) to the code below that I have to copy from all sheets , clearcontents in master sheet before paste then paste , and everytime I am getting an error.

to sum up what Im trying to do, copy contents from all sheets I have in this workbook and paste in in the ARCHIVE sheet, and everytime it pastes I want it to clear contents on the ARCHIVE sheet then paste so that it updates in stead of duplicating value.
VBA Code:
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 M()

Application.OnTime Now + TimeValue("00:00:10"), "CopyToMaster"


End Sub

Im gonna post my code here if anyone can tell me where to put it please.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
VBA Code:
Option Explicit
Sub whitebutterfly()
    Dim ws As Worksheet, LRow As Long, LCol As Long, i As Long
    Set ws = Worksheets("ARCHIVE")
    LRow = WorksheetFunction.Max(ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row, 2)
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    
    'Next line clears the ARCHIVE sheet
    ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).ClearContents
    
    For i = 2 To Sheets.Count
        LRow = Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row
        Sheets(i).Range("A2:N" & LRow).Copy _
        ws.Range("A" & ws.Cells(Rows.Count, 1).End(xlUp).Row + 1)
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top