简体   繁体   中英

Copying values in a column to another column in another worksheet

I have created a button that copies all of the cells in one worksheet to a new worksheet. However I want the Total Quantity (H) column of the first worksheet to go into the Previous Quantity(G) column of the new worksheet. With that I want the Current Quantity (F) column to clear the values. With each new sheet the length of the column could grow so I can't set a specific range.

ActiveSheet.Range("F").ClearContents ??

Anything I've done has had the copy paste commands and I would prefer a direct method. Thanks so much!!!

Would something like this work?

Columns("F:F").Select

Selection.ClearContents

To select an entire column in column F, use

ActiveSheet.Range("F1").EntireColumn.ClearContents

You can also extend this to EntireRow, etc.


Edit - now that I'm clear on what you're asking.

If you already have a button that copies the entire worksheet to another one, then I assume that it copies all the original data as well. I don't know exactly what's on your worksheets, but the following code should show you how to copy & paste entire columns.

Assume there are two columns, G and H. The first row of G is named "Previous" and the first row of H is named "Current". The following code replaces column G with column H while preserving the column names in the first row:

Sub Test()

    'Clear Column G:
    Range(Cells(2, 7), Cells(Rows.Count, 7)).ClearContents

    'Copy & Paste H to G:
    Range(Cells(2, 8), Cells(Rows.Count, 8)).Copy
    Range("G2").Select
    ActiveSheet.Paste

    'Clear Column H:
    Range(Cells(2, 8), Cells(Rows.Count, 8)).ClearContents

End Sub

This is Copy and Paste. If you have no formulas that need preserving, you can get around all that code by simply Cut and pasting:

Sub Test()
    Range(Cells(2, 8), Cells(Rows.Count, 8)).Cut
    Range("G2").Select
    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