SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-2249298%22%20slang%3D%22en-US%22%3Eclearing%20out%20empty%20range%20in%20worksheet%20(causing%20headache%20importing%20into%20MSSQL%20table)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2249298%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20workbook%20with%20some%20sheet%20tabs%20that%20have%20a%20large%20range%20of%20empty%20cells%20that%20I%20cannot%20remove.%20Using%20CTRL%20%2B%20END%20takes%20me%20to%20an%20odd%20location%2C%20well%20below%20my%20existing%20data.%20I%20tried%20deleting%20rows%20below%20my%20data.%20That%20made%20no%20difference%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20found%20a%20post%20on%20this%20forum%20(%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Foffice%2Flocate-and-reset-the-last-cell-on-a-worksheet-c9e468a8-0fc3-4f69-8038-b3c1d86e99e9%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Foffice%2Flocate-and-reset-the-last-cell-on-a-worksheet-c9e468a8-0fc3-4f69-8038-b3c1d86e99e9%3C%2FA%3E)%20that%20I%20thought%20would%20solve%20my%20problem%2C%20but%20it%20did%20not%20work%20as%20advertised.%20selecting%20the%20empty%20range%2C%20clearing%20all%2C%20saving%2C%20closing%20and%20reopening%20the%20file%2C%20and%20I%20sill%20have%20that%20empty%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20particularly%20troublesome%20when%20using%20the%20data%20import%20wizard%2C%20to%20pull%20a%20sheet%20into%20MS%20SQL.%20I%20end%20up%20with%20an%20overly%20large%20table%20with%20mostly%20NULLs.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20using%20Excel%20(Office)%2010%20over%20Windows%2C%20running%20on%20Windows%20Server%202008.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2249298%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2249470%22%20slang%3D%22en-US%22%3ERe%3A%20clearing%20out%20empty%20range%20in%20worksheet%20(causing%20headache%20importing%20into%20MSSQL%20table)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2249470%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1014995%22%20target%3D%22_blank%22%3E%40StephenWallace777%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECtrl%2BEnd%20will%20still%20detect%20the%20earlier%20last%20used%20cell%20on%20the%20sheet%20unless%20you%20save%20your%20file%20after%20deleting%20the%20rows.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20create%20a%20backup%20of%20your%20file%20and%20then%20run%20this%20macro%20and%20see%20if%20your%20issue%20gets%20resolved.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20DeleteEmptyRows()%0ADim%20ws%20%20%20%20%20%20As%20Worksheet%0ADim%20lr%20%20%20%20%20%20As%20Long%0ADim%20elr%20%20%20%20%20As%20Long%0ADim%20sRow%20%20%20%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0ASet%20ws%20%3D%20ActiveSheet%20%20%20%20'Set%20the%20Worksheet%20as%20per%20your%20requirement%2C%20currenty%20it%20is%20set%20to%20the%20ActiveSheet.%0A%0AOn%20Error%20Resume%20Next%0Alr%20%3D%20ws.Cells.Find(%22*%22%2C%20SearchOrder%3A%3DxlByRows%2C%20SearchDirection%3A%3DxlPrevious).Row%0Aelr%20%3D%20ws.Cells.SpecialCells(xlCellTypeLastCell).Row%0AOn%20Error%20GoTo%200%0A%0AIf%20lr%20%26gt%3B%200%20And%20lr%20%26lt%3B%26gt%3B%20elr%20Then%0A%20%20%20%20sRow%20%3D%20lr%20%2B%201%0A%20%20%20%20ws.Rows(sRow%20%26amp%3B%20%22%3A%22%20%26amp%3B%20elr).ClearFormats%0A%20%20%20%20ws.Rows(sRow%20%26amp%3B%20%22%3A%22%20%26amp%3B%20elr).EntireRow.Delete%0A%20%20%20%20ThisWorkbook.Save%0AEnd%20If%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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 below my data. That made no difference

 

I found a post on this forum (https://support.microsoft.com/en-us/office/locate-and-reset-the-last-cell-on-a-worksheet-c9e468a8-0f...) that I thought would solve my problem, but it did not work as advertised. selecting the empty range, clearing all, saving, closing and reopening the file, and I sill have that empty range.

 

This is particularly troublesome when using the data import wizard, to pull a sheet into MS SQL. I end up with an overly large table with mostly NULLs.

 

I am using Excel (Office) 10 over Windows, running on Windows Server 2008.

3 Replies
best response confirmed by StephenWallace777 (New Contributor)
Solution

@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

 

 

 

 

@Subodh_Tiwari_sktneer , thank you for this. Indeed, I had not saved after deleting rows (only saved after the clear all suggestion of the earlier post I mentioned). Deleting rows and saving solved the problem. And very nice Macro! I noticed I still had to save the worksheet after the macro finished. Even adding an additional "ThisWorkbook.Save" just before the End Sub didn't prevent CTRL + END from finding the end of that old empty range. Once I saved manually, the sheet was fine and the phantom range was gone.

You're welcome @StephenWallace777! Glad this resolved your issue.

 

Please take a minute to accept the post with the proposed solution as a Best Response to mark your question as Solved.