简体   繁体   中英

How to clear a sheet and paste data onto it from another sheet

So I have 4 sheets that are called "old", "current", "input", and "buttons". The process is to: press a button on the "buttons" sheet to clear the "current" sheet and "input" sheet, paste data onto the "input" sheet and press a macro button on the "buttons" sheet to populate the "current" sheet. Most of the macro will be formatting the "current" sheet and using index match for data from the "old" sheet. What I'm trying to do is add a step in the beginning to clear the "old" sheet and then copy and paste the data from the "current" sheet onto the "old" sheet. The reason is that I will be using this weekly and every time I run the macro, I want the "current" sheet, that was created last time I ran the macro, to move to the "old" sheet. This is currently the code that I have...

Sub Load16()

Application.ScreenUpdating = False

'Define Workbooks
Dim loopCount As Integer
Dim loopEnd As Integer
Dim writeCol As Integer
Dim matchRow As Integer
Dim writeRow As Integer
Dim writeEnd As Integer

loopEnd = WorksheetFunction.CountA(Worksheets("Input").Range("A:A"))
writeEnd = WorksheetFunction.CountIf(Worksheets("Input").Range("L:L"), "-1")
loopCount = 1
writeRow = 1

Worksheets("Buttons").Range("F17:I17").Copy
Worksheets("Current").Range("J2:M" & writeEnd).PasteSpecial Paste:=xlPasteAll
Application.CutCopyMode = False

Do While loopCount <= loopEnd

If Worksheets("Input").Cells(loopCount, 12).Value <> "" And     
Worksheets("Input").Cells(loopCount, 12).Value <> 0 Then

Worksheets("Current").Cells(writeRow, 1).Value = Worksheets("Input").Cells(loopCount, 26).Value

writeCol = 2
Do While writeCol <= 9
    Worksheets("Current").Cells(writeRow, writeCol).Value = Worksheets("Input").Cells(loopCount, writeCol - 1)
    writeCol = writeCol + 1
Loop

writeCol = 14
Do While writeCol <= 30
    Worksheets("Current").Cells(writeRow, writeCol).Value = Worksheets("Input").Cells(loopCount, writeCol - 5)
    writeCol = writeCol + 1
Loop

Worksheets("Current").Cells(writeRow, 31).Value = Worksheets("Input").Cells(loopCount, 27)
writeRow = writeRow + 1
Else
End If

loopCount = loopCount + 1
Loop

Worksheets("Current").Range("J1").Value = "Counsel"
Worksheets("Current").Range("K1").Value = "Background"
Worksheets("Current").Range("L1").Value = "Comments"
Worksheets("Current").Range("M1").Value = "BM Action"

Lookup Data for K - M and a few other things
loopCount = 2
Do While loopCount <= loopEnd

matchRow = 0
On Error Resume Next
matchRow = WorksheetFunction.Match(Worksheets("Current").Cells(loopCount, 1).Value, _
Worksheets("Old").Range("A:A"), 0)
If matchRow = 0 Then
    Else
    Worksheets("Current").Cells(loopCount, 11).Value =    Worksheets("Old").Cells(matchRow, 11).Value
    Worksheets("Current").Cells(loopCount, 12).Value =  Worksheets("Old").Cells(matchRow, 12).Value
    Worksheets("Current").Cells(loopCount, 13).Value =   Worksheets("Old").Cells(matchRow, 13).Value
End If

Worksheets("Current").Cells(loopCount, 10).Value =   
Worksheets("Current").Cells(loopCount, 18).Value

loopCount = loopCount + 1
Loop

Sheets("Current").Range("A2:AE" & loopEnd).Sort Key1:=Sheets("Current").Range("H2"), _
Order1:=xlAscending, Header:=xlNo

Worksheets("Current").Columns("A:BZ").AutoFit

Application.ScreenUpdating = True

Worksheets("Buttons").Select

MsgBox loopEnd - 1 & " Rows processed.  " & writeEnd & " Rows remain." 

End Sub

Thanks guys.

A small function like this should do the trick.

Sub copy_current_data()

    'Select Old Sheet
    Sheets("Old").Select

    'Clear all cells from Old Sheet
    Sheets("Old").Cells.ClearContents

    'Copy Cells from Current Sheet
    Sheets("Current").Cells.Copy

    'Select "A1" in old sheet
    Sheets("Old").Range("A1").Select

    'Paste Data
    ActiveSheet.Paste

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM