问题
What do I want is to copy an entire row's contents and formatting to another sheet.
At the moment I've had to settle for setting the old cells contents to the new cells contents and in doing so it only copies the contents and not the formatting. (my cells have different colours that need to be carried across)
At the moment I have the following: (this works fine for cells in the same sheet)
Range(Cells(45, 2), Cells(45, 3)).Copy Range(Cells(50, 2), Cells(50, 3))
However, I'm trying to do it from one sheet to another. (Copy from sheet 'Front_Page' to 'vg'). I tried using the following, obviously it doesn't work, but can someone please tell me what am I doing wrong?
Range.Worksheet("Front_Page").Range(Cells(45, 2), Cells(45, 3)).Copy Worksheet("vg").Range(Cells(50, 2), Cells(50, 3))
回答1:
Looks like you try to copy cells from "Front_Pages" to "vg" since you use "cells" inside "range"
Range (cells ...).
If so, you can simply change the cell format as excel general range; try this code :
Sheets("vg").Range("B5") = Sheets("Front_Pages").Range("B4")
Sheets("vg").Range("C5") = Sheets("Front_Pages").Range("C4")
回答2:
Cells refers to cells of active sheet. Thus you get the error: vg is not the active sheet. Specifying cells of another sheet as parameters to Range object always leads to an error. This will work:
Worksheets("Front_Page").Range(Worksheets("Front_Page").Cells(45, 2), Worksheets("Front_Page").Cells(45, 3)).Copy Worksheets("vg").Range(Worksheets("vg").Cells(50, 2), Worksheets("vg").Cells(50, 3))
However, it can be optimized to just:
Worksheets("Front_Page").Range("B45:C45").Copy Worksheets("vg").Range("B50:C50")
Also, notice that Worksheet("vg") doesn't work, it should be replaced with Worksheets("vg") else it will cause error too.
To copy entire row, use:
Worksheets("Front_Page").Rows("45:45").Copy Worksheets("vg").Rows("50:50")
来源:https://stackoverflow.com/questions/9153853/copy-a-rows-contents-and-formatting-to-another-sheet