Getting runtime error 1004 method paste of object _worksheet failed for below code
Please help, thanks in Advance.
Sub Move()
Dim lastrow As Long, erow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastrow
Sheet1.Cells(i, 1).Copy
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Paste Destination = Worksheets("Sheet2").Cells(erow, 1)
Sheet1.Cells(i, 2).Copy
Sheet1.Paste Destination = Worksheets("Sheet2").Cells(erow, 2)
Sheet1.Cells(i, 3).Copy
Sheet1.Paste Destination = Worksheets("Sheet2").Cells(erow, 3)
Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit`
Range("A1").Select
End Sub
Move some thing around and do the whole in one line:
Sub Move()
Dim lastrow As Long, erow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Range(Sheet1.Cells(1, 1),Sheet1.Cells(lastrow , 3)).Copy Sheet2.Cells(erow, 1)
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
Range("A1").Select
End Sub
now if that does not work the perhaps the code names are incorrect and one should replace all the Sheet1 and Sheet2 with Worksheets("Sheet1") and Worksheets("Sheet2") respectively.
along the lines of #ScottCraner 's solution, you could manage sheet references with the use of With ...End With block and avoid multiple repetitions:
Sub Move()
With Sheet1 ' reference Sheet1 once and for all...
.Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 3).Copy Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) ' all Range objects beginning with dots are referencing the object after `With` keyword (i.e. Sheet1)
End With
Sheet2.Columns.AutoFit
End Sub
BTW, using Destination parameter of Copy method you don't "activate" CutCopyMode, hence no need to set it to False
and should you be interested in copying/pasting values only, then nest another With ...End With block to reference the "source" range:
Sub Move()
With Sheet1 ' reference Sheet1 once and for all...
With .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 3) ' reference Sheet1 columns A:C range from row 1 down to column A last not empty cell
Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count).Value = .Value ' get Sheet2 range as its column A last not empty cell, resize it as last referenced range and fill it with its values
End With
End With
Sheet2.Columns.AutoFit
End Sub
Related
Sub copyNonblankData()
Dim erow As Long, lastrow As Long, i As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
If Sheet1.Cells(i, 1) <> "" Then
' i'm assuming the next line is the 8th line?
Sheets("Sheet1").Range(Cells(i, 1), Cells(i, 2)).Copy
Sheets("Sheet2").Activate
' error occurs here
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row.Offset(1, 0).Row
ActiveSheet.Paste Destination:=Sheets("Sheet2").Range(Cells(erow, 1), Cells(erow, 2))
Sheets("Sheet1").Activate
End If
Next i
Application.CutCopyMode = False
End Sub
The Offset property in Excel VBA takes the range which is a particular number of rows and columns away from a certain range.
Sheet2.Cells(Rows.Count, 1).End(xlUp).Row will give you Long and not a Range Object.
Your code can be written as
Dim rng As Range
Set rng = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
erow = rng.Row
Or simply as
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1
Also your code will work if the relevant sheet is active. You need to fully qualify your cells as shown in Why does Range work, but not Cells?
TIP
If you are using CodeName then use CodeName and avoid using the Name of the sheet. And if you are using Name then avoid using Codename. You will end up getting confused. If you do not know the difference between them, then you may want to see Refer to sheet using codename
Remember you will get the same error (Object required) again if the Codename doesn't exist. BTW if the Name of the sheet doesn't exist then you will get a different error (Subscript Out Of Range).
so if erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1 gives the same error then that means Sheet2 doesn't exist. Try with
erow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1
I have recorded a Macro in Excel 2016, however whenever I run this Macro it overwrite my previous answer, how should I put my new answer to next row?
Sub update()
'
' update Macro
'
'
Range("B4:G4").Select
Selection.Copy
Sheets("Sheet4").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Sheet3").Select
Range("B7:G7").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("B3").Select
ActiveSheet.Paste
End Sub
Just add lastRow1 line to set always lastRow +1 (empty cell)
Sub updateM()
Dim lastRow1 As Long
lastRow1 = Sheets("Sheet4").Cells(Rows.Count, 2).End(xlUp).row + 1
Range("B4:G4").Copy Sheets("Sheet4").Range("B2")
Sheets("Sheet3").Range("B7:G7").Copy Sheets("Sheet4").Range("B3").Select
End Sub
This should do it. You should try to a void Select or Activate as Pᴇʜ said, here is an example on how to do it:
Option Explicit
Sub update()
'
' update Macro
'
'
Dim Answer1 As Range, Answer2 As Range
With ThisWorkbook.Sheets("sheet3")
Set Answer1 = .Range("B4:G4") 'set the range for the first answer
Set Answer2 = .Range("B7:G7") 'set the range for the second answer
End With
Dim LastRow As Long
With ThisWorkbook.Sheets("Sheet4")
'Copy the first answer
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 'calculate next blank cell
Answer1.Copy .Cells(LastRow, 2)
'Copy the second answer
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1 'calculate next blank cell
Answer2.Copy .Cells(LastRow, 2)
End With
End Sub
Based on the text ("SNV") present in column L of the "HiddenSheet" worksheet, I would like to select and copy cells in columns 1 to 6 for all rows for which the "SNV" text is present in column L.
Then I would like to paste the values of the copied cells in the SNVReports worksheet.
Sub Macro2()
a = Worksheets("HiddenSheet").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To a
If Worksheets("HiddenSheet").Cells(i, 12).Value = "SNV" Then
Worksheets("HiddenSheet").Range(Cells(i, 1), Cells(i, 6)).Copy
Worksheets("SNVReports").Activate
b = Worksheets("SNVReports").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("SNVReports").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("HiddenSheet").Activate
End If
Next
Application.CutCopyMode = False
End Sub
I sometimes receive:
"Application-defined or object-defined error"
and it is apparently related to my range:
Worksheets("HiddenSheet").Range(Cells(i, 1), Cells(i, 6)).Copy
Your Cells(i,#) references aren't qualified. So if the SNVReports tab is active when the macro runs, it's confused as to what range you're talking about.
The whole code could do with a tidy-up:
Sub Macro2a()
Dim sourcesheet As Worksheet
Dim destsheet As Worksheet
Dim lastsourcerow as Long
Dim lastdestrow as Long
Dim i as Long
Set sourcesheet = Worksheets("HiddenSheet")
Set destsheet = Worksheets("SNVReports")
With sourcesheet
lastsourcerow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 1 To lastsourcerow
If .Cells(i, 12).Value = "SNV" Then
lastdestrow = destsheet.Cells(destsheet.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(i, 1), .Cells(i, 6)).Copy destsheet.Cells(lastdestrow + 1, 1)
End If
Next
End With
End Sub
I'm new with Macro and I want to create a simple copy and paste excel formula from one sheet to another. But the thing is the main data has a formula inside the cell and it wont let me copy and paste as values it to another cell.
Sub selectpasting()
Dim Lastrow As Long, erow As Long
Lastrow = Sheets("attendance").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To Lastrow
If Sheets("attendance").Cells(i, 3) = "absent" Then
Sheets("attendance").Cells(i, 1).copy
erow = Sheets("forpasting").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheets("attendance").Paste Destination:=Sheets("forpasting").Cells(erow, 1)
Sheets("attendance").Cells(i, 3).copy
Sheets("attendance").Paste Destination:=Sheets("forpasting").Cells(erow, 2)
End If
Next i
Application.CutCopyMode = False
Sheets("forpasting").Columns.AutoFit
Range("A1").Select
End Sub
Change this row:
Sheets("attendance").Paste Destination:=Sheets("forpasting").Cells(erow, 1)
To:
Sheets("forpasting").Cells(erow, 1).PasteSpecial xlValues
The complete code would be:
Sub selectpasting()
Dim Lastrow As Long, erow As Long
Dim i As Long
Lastrow = Sheets("attendance").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To Lastrow
If Sheets("attendance").Cells(i, 3) = "absent" Then
Sheets("attendance").Cells(i, 1).Copy
erow = Sheets("forpasting").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheets("forpasting").Cells(erow, 1).PasteSpecial xlValues
Sheets("attendance").Cells(i, 3).Copy
Sheets("forpasting").Cells(erow, 2).PasteSpecial xlValues
End If
Next i
Application.CutCopyMode = False
Sheets("forpasting").Columns.AutoFit
Range("A1").Select
End Sub
The code above is quite slow (try both the codes and you would notice that the below is way faster).. The reason is that in the above excel needs to determine/evaluate if the the cell properties needs to be pasted or not due to ".copy". It's one approach when you need to copy/paste cell formats etc.
In your case you only interested in the value the cells shows. So you could just pick the value and copy it.
I would therefore recommend you to change it to:
Sub selectpasting_2()
Dim Lastrow As Long, erow As Long
Dim i As Long
Lastrow = Sheets("attendance").Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To Lastrow
If Sheets("attendance").Cells(i, 3) = "absent" Then
erow = Sheets("forpasting").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheets("forpasting").Cells(erow, 1) = Sheets("attendance").Cells(i, 1)
Sheets("forpasting").Cells(erow, 2) = Sheets("attendance").Cells(i, 3)
End If
Next i
Application.CutCopyMode = False
Sheets("forpasting").Columns.AutoFit
Range("A1").Select
End Sub
My process consists:
Going through the cell values in Column A of sheet 1
Checking to see if the cell values from sheet 1 match with any of the values in Column C of sheet 2
If there is a match, copy the entire row in which there is a match from Sheet 2 to Sheet 3.
I posted my code below but somehow can't get it to work.
Sub Test1()
Dim Name As String
Dim lastrow As Long
Dim Cell As Variant
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Name = Cells(i, 1)
If Name <> "" Then
For Each Cell In Sheets("Sheet2").Range("C2:C4000")
If Cell.Value = Name Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Sheet3").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Sheet2").Select
End If
Next
End If
Next
End Sub
No need to loop through every cell in Sheet2!C:C.
Sub Test1()
Dim i As Long, c as variant
With Worksheets("Sheet1")
For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
c = Application.Match(.Cells(i, "A").Value2, Worksheets("Sheet2").Columns(3), 0)
If Not IsError(c) Then
Worksheets("Sheet2").Rows(c).Copy _
Destination:=Worksheets("Sheet3").Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End If
Next i
End With
End Sub
You need to get the .Value of the cell.
Name = CStr(Cell(i, 1).Value)
Also, there is a built in function to determine if a cell is empty.
If Not IsEmpty(Cell(i, 1).Value) Then
Also, I would suggest setting a reference to the worksheet instead of just saying Cells()
Dim ws As Worksheet
Set ws = Excel.Application.ThisWorkbook.Worksheets("wb name here")
ws.Cells(i, 1).Value
Hope this helps!
Where your errors were coming from was it was getting confused what sheet was selected. So you needed to be more explicit, as below.
Sub Test1()
Dim Name As String
Dim lastrow As Long
Dim Cell As Variant
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
Name = Sheets("Sheet1").Cells(i, 1)
If Name <> "" Then
For Each Cell In Sheets("Sheet2").Range("C2:C4000")
If Cell.Value = Name Then
matchRow = Cell.Row
Sheets("Sheet2").Select
ActiveSheet.Rows(matchRow).Select
Selection.Copy
Sheets("Sheet3").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("Sheet2").Select
End If
Next
End If
Next
End Sub