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

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

Related

VBA - Pasting/Inserting a range without formatting shifted down

This is probably pretty simple but as a beginner I am not sure how to combine these two codes into one.
Right now I use this code to copy and paste a table into specific range.
Sheet3.Range("MyTable").Copy
Sheets("MyWorksheet").Range("B12:D12").Insert Shift:=xlDown
Now I want to paste it without formatting and this code should do the work:
Sheets("MyWorksheet").Range("B12:D12").PasteSpecial xlPasteValuesAndNumberFormats
All I need to do is add the Shift:=xlDown to the second code somehow... Is it possible to do it all in one line?
Try with this solution where 1st- we copy + insert as previously, 2nd- we copy to paste with values to overwrite what was inserted:
Sheet3.Range("MyTable").Copy
Sheets("MyWorksheet").Range("B12:D12").Insert Shift:=xlDown
With Sheets("MyWorksheet").Range("B12").Resize(Sheet3.Range("MyTable").Rows.Count, Sheet3.Range("MyTable").Columns.Count)
.Copy
.PasteSpecial xlPasteValuesAndNumberFormats
End With
Application.CutCopyMode = False
Try to do it in two lines but you have to keep the right order of all instructions:
Sheets("MyWorksheet").Range("B12:D12").Insert Shift:=xlDown
Sheet3.Range("MyTable").Copy
Sheets("MyWorksheet").Range("B12").PasteSpecial xlPasteValuesAndNumberFormats
It could be also required the following line before .insert:
application.CutCopyMode = false

Excel VBA Copy from one worksheet to a new one whilst keeping formatting and values

I'm trying to copy a table from one worksheet to new one created when running the macro whilst keeping the data formatting and cell formatting. I've used the following code which will copy from the worksheet to a specified one, and keeps most of the formatting apart from the cell heights and width.
Any help would be greatly appreciated.
Sheets("Sheet1").Range("A1:B10").Copy Destination:=Sheets("Sheet2").Range("E1")
Sheets("Sheet1").Range("A1:B10").Copy
With Sheets("Sheet2").Range("E1")
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With

Copy everything in a worksheet vba

' 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
This code allows me to copy everything in a range of A1:W79. How can I modify this Range so that it selects everything that contains value in a worksheet. For example, worksheet might contain values from C7:G20 etc.
I'd go like follows:
With wb.Sheets(wsSource.Name).UsedRange
.Copy
With wbTarget.Sheets("Sheet1").Range(.Address)
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With

Copy and paste format including colour of cells VBA Excel

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

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