Runtime error 1004 when using Cut and PasteSpecial - vba

I have to cut the contents from active cell for a specific range in sheet 1 and paste it into the active cell of that range in sheet 2. Cutting the specific range of contents is working fine, but in the paste options I am getting runtime error 1004 as application defined or object defined error.
Here is the code I am using:
Sub sheet1_sheet2_copy_click()
Sheets("sheet1").Activate
ActiveCell.Resize(1, 26).Cut
Sheets("sheet2").Activate
ActiveCell.Resize(1, 26).PasteSpecial
End Sub

Your question is a little unclear, but you can try doing this:
Sub sheet1_sheet2_copy_click()
Sheets("sheet1").Cells(1, 26).Cut
Sheets("sheet2").Cells(1, 26).PasteSpecial Paste:=xlPasteValues
End Sub
Better yet, you can just do this:
Sub sheet1_sheet2_copy_click()
Sheets("sheet2").Cells(1, 26).value = Sheets("sheet1").Cells(1, 26).Value
End Sub
In general, it is better to not use .Activate or .Select, but to instead explicitly define your references like I showed. Modify the Paste:= to whatever parameter you are trying to use for PasteSpecial.
See https://msdn.microsoft.com/VBA/Excel-VBA/articles/range-pastespecial-method-excel for more information on this command.

Personally, I think using the active cell as a source and destination is rather unreliable, but if you change the paste line to activesheet.paste it should work fine.

I have tested your code and it looks like PasteSpecial only works if you use Copy instead of Cut, so you should modify your code this way:
Sheets("sheet1").Activate
ActiveCell.Resize(1, 26).Copy
Sheets("sheet2").Activate
ActiveCell.Resize(1, 26).PasteSpecial
If you need to delete the data from sheet1, then do it after the PasteSpecial.

Related

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

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

Copy formula from a specific sheet and cell and drag it down until specific column last row

So far I have this code below; counting last cell is working fine but is copy/pasting the wrong data to wrong sheet. Should copy data and use the formula from Sheet "Parsing" cell B2, and its using the main sheet where is the VBA. Looks lile what is missing is to execute the copy/select to "parsing" sheet, but didnt manage to do it.
Sub drag_formula_original()
Dim myLastRow As Long
With Worksheets("Parsing")
myLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("B2").Copy Destination:=.Range("B2:B" & myLastRow)
Application.CutCopyMode = False
End With
End Sub
Its solved. Thanks a lot.
Range("B2").Copy
The above will grab by default from the Activesheet
you have to tell it what sheet you would like it to pick that range/value from.
sheets("Parsing").Range("B2").Copy
Edit: Just noticed your With
To actually use a with you need to use a "." e.g. your copy line would look like below
.Range("B2").Copy
One other thing to note this:
Range("B2:B" & myLastRow).Select
ActiveSheet.Paste
Is rather inefficient, below would be better. Selecting in general is best to keep away from it is rather slow
Range("B2:B" & myLastRow).Paste
Or with your with
.Range("B2:B" & myLastRow).Paste
I just copied and pasted your code and ran it. I changed nothing in your code except for adding "Option Explicit" before your sub. (Just a personal habit)
Option Explicit
Sub drag_formula_original()
Dim myLastRow As Long
With Worksheets("Parsing")
myLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Range("B2").Copy
Range("B2:B" & myLastRow).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End With
End Sub
I did, however, use a very simple formula in cell B2. What I did was have column A go from 1 to 10 and column C go from 11 to 20. Cell B2 was =A2+C2.
After running the code I checked each cell in column B and they each had the correct formula in them, and not a hard-coded value.
A trick I do when I want to do something like this but can't figure out how is I record a macro of me dragging the cell formula down a little ways and then stop the recording and look at the code it made. From that you should be able to adjust it to do what you want.
When I did that I got this code:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B15"), Type:=xlFillDefault
Range("B2:B15").Select
End Sub

Run time Error - 438

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.

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")