Forum Discussion

StephenWallace777's avatar
StephenWallace777
Copper Contributor
Apr 01, 2021
Solved

clearing out empty range in worksheet (causing headache importing into MSSQL table)

I have a workbook with some sheet tabs that have a large range of empty cells that I cannot remove. Using CTRL + END takes me to an odd location, well below my existing data. I tried deleting rows be...
  • StephenWallace777 

     

    Ctrl+End will still detect the earlier last used cell on the sheet unless you save your file after deleting the rows.

     

    First create a backup of your file and then run this macro and see if your issue gets resolved.

     

    Sub DeleteEmptyRows()
    Dim ws      As Worksheet
    Dim lr      As Long
    Dim elr     As Long
    Dim sRow    As Long
    
    Application.ScreenUpdating = False
    Set ws = ActiveSheet    'Set the Worksheet as per your requirement, currenty it is set to the ActiveSheet.
    
    On Error Resume Next
    lr = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    elr = ws.Cells.SpecialCells(xlCellTypeLastCell).Row
    On Error GoTo 0
    
    If lr > 0 And lr <> elr Then
        sRow = lr + 1
        ws.Rows(sRow & ":" & elr).ClearFormats
        ws.Rows(sRow & ":" & elr).EntireRow.Delete
        ThisWorkbook.Save
    End If
    Application.ScreenUpdating = True
    End Sub

     

     

     

     

Resources