StuReaRitchie1982
New Member
- Joined
- Dec 19, 2016
- Messages
- 1
Apologies if the answer to this is somewhere within the forum already - I've searched high and low, copying bits and bats from here and there and still the error I get keeps appearing.
I am trying to finalise my code for inserting copied data into a user selected row but within a different worksheet.
Forgive me if the code seems all over the place, I am learning from scratch and as I say, am copying from other codes and deciphering it and using trial and error. I have succeeded so far and to be fair the whole code below works fine when inserting the copied data onto the same sheet as the original data and command button, but just shows a run-time 1004 error (Insert Method of Range class failed) when running the below macro via another button with the same macro applied, but on the destination sheet.
Sub PasteRow()
Dim sh As Worksheet
Set sh = ActiveSheet
'select the A column of the chosen row (if the used chooses a cell in row C or D for instance)
ActiveSheet.Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 1)).Select
With ActiveSheet
'insert the copied data into the selected row (user selected)
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove <------- Error Appears on this line all the time
'Below code reapplies the protection to the workbook which works fine
Dim myPassword As String
myPassword = ""
For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=myPassword
Next sh
End With
End Sub
As I say the code works fine when running within one sheet over and over but I need the data to be able to be inserted into another sheet, of which there a numerous different destination sheets and rows which the user could choose!
Any help would be great.
Thanks!
I am trying to finalise my code for inserting copied data into a user selected row but within a different worksheet.
Forgive me if the code seems all over the place, I am learning from scratch and as I say, am copying from other codes and deciphering it and using trial and error. I have succeeded so far and to be fair the whole code below works fine when inserting the copied data onto the same sheet as the original data and command button, but just shows a run-time 1004 error (Insert Method of Range class failed) when running the below macro via another button with the same macro applied, but on the destination sheet.
Sub PasteRow()
Dim sh As Worksheet
Set sh = ActiveSheet
'select the A column of the chosen row (if the used chooses a cell in row C or D for instance)
ActiveSheet.Range(Cells(ActiveCell.Row, 1), Cells(ActiveCell.Row, 1)).Select
With ActiveSheet
'insert the copied data into the selected row (user selected)
Selection.EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove <------- Error Appears on this line all the time
'Below code reapplies the protection to the workbook which works fine
Dim myPassword As String
myPassword = ""
For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=myPassword
Next sh
End With
End Sub
As I say the code works fine when running within one sheet over and over but I need the data to be able to be inserted into another sheet, of which there a numerous different destination sheets and rows which the user could choose!
Any help would be great.
Thanks!