Copying Pasting .... Urgent Help Please

bhagi

New Member
Joined
Jun 27, 2011
Messages
16
Hey
i am getting runtime error 1004 'Application defined or object defined' on running this code can anyone please help... its reallly urgent

Code:
Private Sub CommandButton2_Click()
Dim torow As Long
Dim lastrow As Long
Dim dayCounter As Integer
Dim i, k As Integer

lastrow = Range("A65").End(xlUp).Row

i = Sheet2.Range("A1:A50").Find("Lever 1", Range("A1"), xlValues, xlWhole, xlByColumns, xlNext).Row

i = i + 1


'For dayCounter = 2 To 32

For k = 1 To (lastrow - 1)
 
    Sheet2.Rows(i).Insert Shift:=xlDown

Next

ActiveWorkbook.Sheets(1).Activate
ActiveWorkbook.Sheets(1).Range(Cells(2, "A"), Cells(lastrow, "A")).Select
ActiveWorkbook.Sheets(1).Range(Cells(2, "A"), Cells(lastrow, "A")).Copy
ActiveWorkbook.Sheets(2).Activate
ActiveWorkbook.Sheets(2).Range(Cells((i - 1), "B"), Cells((i + lastrow - 2), "B")).Select
ActiveWorkbook.Sheets(2).Paste


End Sub


Thanks in advance :-)
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
If you hit "debug" when you get that error message, which row of code does it highlight?
 
Upvote 0
ActiveWorkbook.Sheets(2).Range(Cells((i - 1), "B"), Cells((i + lastrow - 2), "B")).Select</pre>
 
Upvote 0
Just before that line, add this line of code and tell me the last values it returns when you run it (it will be a Message Box returning the values of i and lastrow):
Code:
MsgBox "i=" & i & vbCrLf & "lastrow=" & lastrow
 
Upvote 0
if u are thinking the same i too thought it must be because i value is somehow made zero... but its not cuz of dat ...
it says
i = 5
lastrow = 6
 
Upvote 0
It works fine for me, it selects the range B4:B9.
Are any cells in that range protected, meaning it won't let you edit or copy to them?
 
Upvote 0
is it printing the values from A2:A6 of sheet 1 into B4:B9 of sheet 2
Yep, that is exactly what it is doing. Here is your code, dummied down so I could test it.
Code:
Sub TestCode()
 
i = 5
lastrow = 6
    
ActiveWorkbook.Sheets(1).Activate
ActiveWorkbook.Sheets(1).Range(Cells(2, "A"), Cells(lastrow, "A")).Copy
ActiveWorkbook.Sheets(2).Activate
ActiveWorkbook.Sheets(2).Range(Cells((i - 1), "B"), Cells((i + lastrow - 2), "B")).Select
ActiveWorkbook.Sheets(2).Paste
 
End Sub
Does this code work for you?

Did you verify that you do not have any protected sheets or cells on your file? If I protect any one of the cells in the range I am trying to copy to, it returns the "1004" error you described, so I suspect you have a protected cell that you can't write to, but are trying to copy to.
 
Upvote 0
i copy pasted the same code which u tuk as test in a new worksheet
it gave the same thing.

i selected all the cells and unlocked the cells from format cells and tried the same... it gave the same thing

wat do you mean by protected... is it locked
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,911
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top