Help to Clear Contents with Code

keithb

New Member
Joined
Oct 29, 2008
Messages
42
I copied this code and made some changes to fit my needs.
It does what I want it to do as far as coping the rows over to a new sheet, but it won't clear the old data when the code is run.
Can someone show me why it doesn't work?
I am not very good with codes

Keith

Sub CopyRowToPenders()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN AH HAS VALUE "P/IHF" FROM March 2011
' INTO "Penders" SHEET
'========================================================================
Dim LastRowMarch2011, LastRowPenders As Long
Dim i As Long
Application.ScreenUpdating = False
' LastRowPenders = Worksheets("Penders").Range("A" & Rows.Count).End(xlUp).Row
' Sheets("Penders").Range("A2:AM" & LastRowPenders).ClearContents
LastRowMarch2011 = Worksheets("March 2011").Range("A" & Rows.Count).End(xlUp).Row
With Worksheets("March 2011")
For i = 2 To LastRowMarch2011 Step 1
If Cells(i, "AH").Value = "P/IHF" Then
LastRowPenders = Worksheets("Penders").Range("A" & Rows.Count).End(xlUp).Row
Rows(i).Copy Worksheets("Penders").Range("A" & LastRowPenders + 1)
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi Keith,

If you have an apostrophe (') at the start of a line of code excel will skip over it, so if you remove the ' from the start of

Code:
' LastRowPenders = Worksheets("Penders").Range("A" & Rows.Count).End(xlUp).Row
' Sheets("Penders").Range("A2:AM" & LastRowPenders).ClearContents

then hopefully it should work!

If it doesn't or you have any questions please let me know.

Cheers,
alx7000
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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