Code to Copy/Paste based on Criteria

jhedges

Board Regular
Joined
May 27, 2009
Messages
208
I have two worksheets one titled "PL dbase" and the other "waiting list" in the same workbook. I would like the below code, to trim the last entry(ies) from the active sheet. Right now it trims entries from all the sheets. Could someone help me determine how to adjust this code to accomplish this?

Code:
Sub TrimAllSheets()
 
    Dim cs As String
    cs = ActiveSheet.Name
    Dim y As Integer
    y = Application.InputBox("How many bottom rows do you wish to delete?", _
    Default:=1, Type:=1) 'Change default number (3) if desired.
    If MsgBox("Are you sure you wish to delete " & y & " rows from the bottom of ALL sheets?", _
    vbYesNo, "Trim ALL Sheets") = vbNo Then Exit Sub
    Application.ScreenUpdating = False
    Dim r As Range, s As Range
    Dim ws As Worksheet
    On Error Resume Next 'Error handler
    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
        Set r = ActiveSheet.Range("A65536").End(xlUp).Offset(-y + 1)
        Set s = ActiveSheet.Range("A65536").End(xlUp)
        If ActiveCell.Row < 10 Then GoTo circumv 'Not to delete Headers
        Range(r, s).EntireRow.Delete
circumv:
    Next ws
    Sheets(cs).Activate
    Application.ScreenUpdating = True
 
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi,

I am not an expert, but the following works if the last cell value in your range of column A is less than 10.

Give it a go on a non-important test sheet.

Code:
Sub TrimAllSheets()
 
 
    Dim y As Integer
    y = Application.InputBox("How many bottom rows do you wish to delete?", _
    Default:=1, Type:=1) 'Change default number (3) if desired.
    If MsgBox("Are you sure you wish to delete " & y & " rows from the bottom of ALL sheets?", _
    vbYesNo, "Trim Active sheet") = vbNo Then Exit Sub
    Application.ScreenUpdating = False
    Dim r As Range, s As Range
    Dim activesheet As Worksheet
    'On Error Resume Next 'Error handler
    With activesheet
        Range("A65536").Select
        Range(Selection, Selection.End(xlUp)).Select
        ActiveCell.Select
        ActiveCell.Offset(-y + 1).Activate
 
        If ActiveCell.Row < 10 Then GoTo circumv 'Not to delete Headers
        Dim CurRow As Integer, CurCol As Integer, intCount1 As Long
 
 
    CurRow = ActiveCell.Row
 
    Range("a" & CurRow).Delete
 
 
 
        End With
circumv:
 
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,758
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