How to get rid of "ghost" rows without saving the file?

Kyrpo

Board Regular
Joined
Jul 24, 2015
Messages
127
Hi Excel Experts,

I'm currently trying to create a macro to format some data.

The data we receive are usually quite consistent except for some "ghost" rows at the end of the worksheet.

The only way I found to get rid of those is the follow...

Code:
 Dim LR1 As Integer 'last cell used in worksheet
 LR1 = Range("A1").SpecialCells(xlLastCell).Row
 Dim LR2 As Integer 'last cell in region
 LR2 = Range("A1").CurrentRegion.Rows.Count
    
    If LR1 > LR2 Then
    
       Rows(LR1 & ":" & LR2 + 1).delete

Unfortunately... the only way that I've found to make excel receive and accept the input that those "ghost" row have been deleted is to save the file, otherwise even if deleted if I run the same macro again those "ghost rows" will still be there.

Is there any way to make Excel realize that those "ghost" rows have been deleted without saving the file?


Thanks for your support and enjoy your weekend.
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Another option as UsedRange also has a habit of retaining a memory especially when cells were previously formatted (although referring to it seems to work).

Please note the code below is set up to work through all the sheets in the workbook at the moment.

Code:
Sub LoseThatWeight()

    Dim x As Long, Lastrow As Long, LastCol As Long

    Application.ScreenUpdating = False

    On Error Resume Next

    For x = 1 To Sheets.Count
        With Sheets(x)
            Lastrow = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            LastCol = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                                  LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
            .Range(.Cells(1, LastCol + 1), .Cells(Rows.Count, Columns.Count)).Delete
            .Range(.Cells(Lastrow + 1, 1), .Cells(Rows.Count, Columns.Count)).Delete
        End With
    Next x
    On Error GoTo 0
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thanks Mark, if I end up having trouble with the UsedRange function I'll switch to the one you proposed. Thanks again.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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