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
Related
I would like to make a macro which will copy certain cells values marked by user in one sheet to another,but into different cells.
In one worksheet we have data in cells from A1 to D1,my goal is to paste them into second worksheet but to another cells (A2,A4,A6,A8 in my case)
And also when somebody copies for example more than four cells it will also paste them right next (B2,B4,B6,B8 and so on..)
I've managed something like this but it does not work
Sub sbCopyRangeToAnotherSheet()
'Copy the data
Sheets("Arkusz2").Range("A2:D2").Copy
Sheets("Arkusz1").Activate
'Select the target range
Range("A2", "A5", "A8", "A11").Select
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
The correct syntax is
Sub sbCopyRangeToAnotherSheet()
Sheets("Arkusz2").Range("A2:D2").Copy
Sheets("Arkusz1").Range("A2,A5,A8,A11").PasteSpecial
Application.CutCopyMode = False
End Sub
or you can even do it in one line:
Sub sbCopyRangeToAnotherSheet()
Sheets("Arkusz2").Range("A2:D2").Copy Sheets("Arkusz1").Range("A2,A5,A8,A11")
End Sub
And I recommend to read: How to avoid using Select in Excel VBA.
I'm trying to copy the active column and paste it next to it but the code selects the entire worksheet because it has merged cells in.
Sub CopyPaste()
Columns(ActiveCell.Column).Selection
Selection.Copy
ActiveCell.Offset(0,1).PasteSpecial Paste:=xlPasteAll
End Sub
Could you please help me adding the missing code to ignore merged cells?
This is yet another reason to avoid using Select in VBA for Excel. Your selection will expand with the merged cells. You can try this:
ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 1).EntireColumn
And again, you should find some way to avoid counting on the ActiveCell in your code, and use some fully qualified range.
Currently, I am copying the values and formats of a worksheet to a worksheet in another workbook as such:
workbooks("book1.xlsm").sheets(1).range(someRange).copy
with workbooks("book2.xlsm").sheets(1).range("A1")
.pasteSpecial xlPasteValues
.pasteSpecial xlPasteFormats
end with
I would like to use something like:
workbooks("book1.xlsm").sheet(1).copy after:=workbooks("book2.xlsm").sheet(1)
The problem is that some of the cells in sheet(1) of book1.xlsm have formulas. Using the second method pulls the formulas, and the resulting values are linked to the data in book2.xlsm
How can I use the second method, but have the values, not formulas, copied?
If not possible, what are some alternatives, aside from the first method, which I am currently using?
You can break the links after the copy operation:
Option Explicit
Sub copySheetValues()
With Workbooks("Book2")
Workbooks("Book1").Worksheets(1).Copy After:=.Worksheets(1)
Application.Wait Now + TimeValue("0:00:01") 'built-in replacement for "Sleep"
.BreakLink Name:=Workbooks("Book1.xlsm").FullName, Type:=xlLinkTypeExcelLinks
End With
End Sub
Note: both files need to be open in the same instance of the Excel application
I have the following code in which I am trying to copy data from one sheet to another in same workbook. When I run the code I get Runtime error -438
Sub Copy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet2").Activate
Range("E1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Try the following code. You should not rely on Activate and Select.
Sub ZCopy()
Sheets("Sheet1").Range("A1:D20").Copy
Sheets("Sheet1").Paste Destination:=Worksheets("Sheet2").Range("E1")
Application.CutCopyMode = False
End Sub
Interesting Reads
MSDN
How to avoid using Select in Excel VBA macros
Do you have a particular need for copy and paste? This can be slow and inefficient. If you're just copying data from one sheet to another, you can set the values of one range equal to the values of another range and avoid the whole thing.
Sheets("Sheet2").Range("E1:H20").Value = Sheets("Sheet1").Range("A1:D20").Value
This will set the range from cells E1:H20 on Sheet2 to the same values as those in the range A1:D20 on Sheet1, which is effectively a copy and paste. I should add that this will work only for the values themselves.
If there is specific formatting (or formulas) that you need copied and pasted, this method won't work.
I want to copy 10 rows from a workbook into a new workbook. The copy & paste is fine, but in the copied cells are some formulas that I need to replace with the results.
Is there a magical way to copy only the displayed values from the cells?
This is how I do it now:
Rows("2:11").Select
Selection.Copy
myWorkbook.Sheets(1).Activate
ActiveSheet.Rows("1:10").Select
ActiveSheet.Paste
Rows("2:11").Copy
myWorkbook.WorkSheets(1).Rows("1:10").PasteSpecial xlPasteValues
Application.CutCopyMode = False