Copy the contents of the lines and formatting (to another sheet)

What I need is to copy the contents of the entire line and formatting to another sheet.

At the moment, I had to decide to set the contents of the old cells to the contents of the new cells, and in doing so, it only copies the contents, not the formatting. (my cells have different colors that need to be transferred)

I currently have the following: (this works fine for cells on a single sheet)

Range(Cells(45, 2), Cells(45, 3)).Copy Range(Cells(50, 2), Cells(50, 3)) 

However, I am trying to do this from one sheet to another. (Copy from the sheet "Front_Page" to "vg"). I tried using the following, obviously this will not 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)) 
+6
source share
2 answers

It looks like you are trying to copy cells from "Front_Pages" to "vg" since you are using "cells" inside the "range"

Range (cells ...).

If so, you can simply change the format of the cell as a general excel range; try this code:

 Sheets("vg").Range("B5") = Sheets("Front_Pages").Range("B4") Sheets("vg").Range("C5") = Sheets("Front_Pages").Range("C4") 
+1
source

Cells refers to the cells of the active sheet. Thus, you get an error: vg is not an active sheet. Specifying the cells of another sheet as parameters of the 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 only be optimized:

 Worksheets("Front_Page").Range("B45:C45").Copy Worksheets("vg").Range("B50:C50") 

Also note that Worksheet("vg") does not work, it should be replaced with Worksheets("vg") otherwise it will also cause an error.

To copy the entire line, use:

 Worksheets("Front_Page").Rows("45:45").Copy Worksheets("vg").Rows("50:50") 
0
source

Source: https://habr.com/ru/post/907727/


All Articles