Copy and paste format including colour of cells VBA Excel - vba

wbTarget.Sheets("Sheet1").Range("A1:W79").Value = wb.Sheets(wsSource.Name).Range("A1:W79").Value
I have this code which works pasting the values of wb.Sheets(wsSource.Name) to wbTarget sheets. However, it only pastes the value and not the format/color. How do I paste it including the fonts, color of cell.

You need to use Copy, and PasteSpecial xlValues and PasteSpecial xlFormats.
' Copy
wb.Sheets(wsSource.Name).Range("A1:W79").Copy
' Paste Special
wbTarget.Sheets("Sheet1").Range("A1:W79").PasteSpecial xlValues
wbTarget.Sheets("Sheet1").Range("A1:W79").PasteSpecial xlFormats
Read about Range.PasteSpecial here MSDN

Related

How to copy xlCellTypeVisible cells and keep their formatting (bold text)?

I am using this piece of code to copy and paste some stuff:
.SpecialCells(xlCellTypeVisible).Copy
Wsh.Cells(1).PasteSpecial Paste:=8
Wsh.Cells(1).PasteSpecial Paste:=xlPasteAll
Wsh.Cells(1).PasteSpecial Paste:=xlPasteFormats
However, when it pastes it loses the formatting. Some of the text I am copying is bold and I would like it to remain bold. How can I achieve this?
Try a straight Copy direct to destination then come back and pick up the column widths that were left behind.
with .SpecialCells(xlCellTypeVisible)
.copy destination:=Wsh.Cells(1)
.copy
Wsh.Cells(1).PasteSpecial Paste:=xlPasteColumnWidths
end with

Excel VBA - Copy and paste partial formatting

How can I copy and paste a cell from one cell to another that also carries-over formatting on certain texts within the cell using VBA in Excel? For example,
how would I copy "Hello World" so it exactly appears that way in pasted cell?
Thanks!
Create macro and record your steps in Excel.Open the macro and view the VBA code
Range.PasteSpecial Method (Excel)
Option Explicit
Sub PasteSpecial()
'Copy and PasteSpecial a Range
Range("A1").copy
Range("A3").PasteSpecial
Application.CutCopyMode = False
End Sub

Excel Macro Copy Paste from one sheet to other

I have a macro which does few calculation in another workbook and creates a new sheet where it writes the final data.
In the last step, the values from the new sheet should be copied to the current workbook where the Macro is written.
I have written the following line to do the paste activity. But my problem is, each time the data gets pasted in different places in the workbook. Is there a way for me to paste values starting from particular column
ThisWorkbook.Activate
Sheets(1).Select
ActiveSheet.Paste
Of course, if its a static range:
Range("B3:D7").Select 'Or the range you need
Selection.Copy ' Copy that selection
Sheets("yoursheet").Select 'Select the sheet or workbook where you will paste info
ActiveSheet.Paste ' Paste data
Tell me how it goes.
To select the range in destination Sheet use this order :
Sheets("yoursheet").Select
Range("A1").PasteSpecial Paste:=xlPasteValues

How to paste value of a sheet using VBA?

I have always used following to make a sheet to contain only values:
Sheets("NameOfTheTab").Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:= xlPasteValues
But this is not safe, if someone/event change the selection, the program runs into random behaviour. How can can I get rid of this pattern?
Well, for one thing, you can avoid selecting the cells, just directly copy them:
Sheets("NameOfTheTab").Activate
Cells.Copy
Cells(1,1).PasteSpecial Paste:= xlPasteValues
Although this might not be the fastest way, depending on your sheet/actual problem.
You should always avoid using select method, beacuse it is not reliable. The sub below is a sample to copy data from a worksheet and paste only values to another one. This sample sub assumes you are running this macro from your target workbook if not change ThisWorkbook to your target workbook.
Sub copy_paste_only_values()
'will copy all cells in your tab that contain data
ThisWorkbook.Worksheets("NameOfTheTab").Cells.Copy
'will paste only values to your target worksheet
ThisWorkbook.Worksheets("NameOfTheTargetTab").Range("A1")._
PasteSpecial Paste:=xlPasteValues
'empty the clipboard
Application.CutCopyMode = False
End Sub

Paste Excel Range into Powerpoint as Table

I am trying to write get write a macro in excel to copy a range of cells from excel to powerpoint. However, I can't seem to figure out the line to paste the range into powerpoint as a table (like right clicking and hitting 'paste') in powerpoint.
I tried Activesheet.shapes.paste but that gave me an error about data type
I also tried Activesheet.shapes.pastespecial, but none of the options are for pasting as a table as far as I can tell. I do not want to paste as a picture or anything like that.
Any ideas?
James - check this out: http://www.thespreadsheetguru.com/blog/2014/3/17/copy-paste-an-excel-range-into-powerpoint-with-vba
It looks like the relevant code snippet from this post is the following:
'Copy Range from Excel
Set rng = ThisWorkbook.ActiveSheet.Range("A1:D12")
'Copy Excel Range
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
Set myShapeRange = mySlide.Shapes(mySlide.Shapes.Count)