Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA How to copy the content of a cell without .Select

I am writing a method that can take the Target and paste the cell exactly to another cell. The cell is a shipping label with some fancy formatting. Is there a way I can do it?

Originally I have this:

Worksheets("Label").Range("A1").Value = Worksheets("Get Address").Range("A28").Value

It worked for the plain text. However I lost the styling I created, they are different because after the first line, the style is different:

enter image description here

I also tried to use Macro Recorder and I got a solution using .Select, and I read this question not to use it whenever possible. What can I do?

' Created by the Macro Recorder
Range("A28:A33").Select
Range("A33").Activate
Selection.Copy
Sheets("Label").Select
Range("A1").Select
ActiveSheet.Paste
like image 265
George Avatar asked May 22 '13 18:05

George


People also ask

How do I copy only the value of cells?

Click Home > Find & Select, and pick Go To Special. Click Visible cells only > OK. Click Copy (or press Ctrl+C). Select the upper-left cell of the paste area and click Paste (or press Ctrl+V).

How do I copy a cell value in a macro in Excel?

We can copy a value and paste it to another cell. We can use Paste Special to paste only the values. Similarly, in VBA, we use the copy method with range property to copy a value from one cell to another. We use the worksheet function Paste Special or the paste method to paste the value.

How do you avoid selection in Excel VBA?

Probably the biggest thing you can do to avoid using Select is to as much as possible, use named ranges (combined with meaningful variable names) in your VBA code.

How do I copy and paste data in Excel VBA?

We use Dot (.) operator to use the copy and paste methods in VBA. We can copy an entire column and paste it to another column and similarly we can also copy an entire row and paste it to another row.


1 Answers

Worksheets("Get Address").Range("A33").Copy _
       Destination := Worksheets("Label").Range("A1")
like image 68
Tim Williams Avatar answered Sep 20 '22 03:09

Tim Williams