VBA - Pasting/Inserting a range without formatting shifted down - vba

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

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

How to get autofill working in VBA?

I'm trying to get the autofill option to work. For one situation I only need to copy the values in a cell. In the other the formulas (but I guess this works the same as copying a value?).
This selects the cell I want to copy all the way down to the last filled cell in column B
Range("A1048576").End (xlUp)
Just adding the filldown option does not work. Any combination of .Select or .Value doesn't work either.
When I'm trying to simplify it by using this code, the filldown option doesn't work either.
Range("A11").Select
Selection.Filldown
Anybody got an idea?
It's always good if you can use the macro recorder for such tasks and then edit the result to remove the Select and ActiveCell:
Option Explicit
Sub Makro1()
Range("A1").Select
ActiveCell.FormulaR1C1 = "123"
Range("A1").Select
Selection.AutoFill Destination:=Range("A1:A11"), Type:=xlFillDefault
End Sub

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

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

Re: .Copy and .Paste in Excel VBA

Why, when copying a cell, shape, etc in Excel VBA, is ActiveSheet.Cells(i,j).Paste not valid?
Instead I have
Cells(i,j).Select
ActiveSheet.Paste
which works, but why?
Because .Paste has to be applied to a SheetObject (as you have written correctly: ActiveSheet.Paste)
Check out the method on MSDN. It has to be used the following way:
Worksheets("Sheet1").Range("C1:C5").Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("D1:D5")
Or more shortly:
Cells(j,i).Copy Destination:=Cells(y,z)
Or use the PasteSpecial-method. It can be applied to Range-objects:
With sheet
.Range("C1:C5").Copy
.Range("D1:D5").PasteSpecial Operation:=xlPasteSpecialOperationAdd
End With
You don't have to tell it to paste for example,
if you have the variables set for i and j then you can use this
Range("A1").Copy Cells(i, j)
You could even have no copying at all, such as:
Cells(i, j)=Range("A1")