Newbie here on Excel VBA. I would just like to ask how to fix the error I'm getting? I'm trying to copy an entire row base on a criteria, and paste it on another sheet (same workbook). Here's my code:
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim endRow As Range
Set ws = Sheets("FIELD OFFICE DATABASE")
Set ws1 = Sheets("Transferred Items")
set endRow = Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
ws.Activate
ws.Unprotect "321321"
Range("B2").Select
Do Until endRow
If ActiveCell.Value = Me.cmbemn.Text Then
ActiveCell.Rows("B:S").Select
Selection.copy
ws1.Activate
ws1.Unprotect "321321"
endRow.Select
ActiveSheet.Paste
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
What happens is its either my Excel crashes, or an error pops up saying "Object required". But mostly it crashes.
Please help! Thanks!
Unprotecting your worksheet cancels the copy/paste operation.
Protect your worksheets with the following parameter,
Now you can do anything you want to do to them in VBA without unprotecting them
Try changing the loop termination condition to something like:
I think you could terminate earlier with:
You also should be explicit in which worksheet you are working with or else the currently activesheet will be used. I have assumed you are working with
ws
.And
Rows
should not specify columns within it:It should have a Rows specification. Potentially you could do something like:
Or as per later comment,
There doesn't need to be a
.Select
then a second line for the copy. You can do in one line.Note also @Jeeped's comment on
Unprotect
.My preference would be to specify a loopRange e.g.
and then loop over that range.
This has the added benefit of getting rid of the use of activecell.