Home » excel » Delete Excel row without unhiding another, VBA

Delete Excel row without unhiding another, VBA

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a fairly simple spreadsheet application with some VBA code that performs simple row inserts and deletes as called by a user. This is a heavily formatted spreadsheet intended for use by people without Excel experience. The code for inserts and deletes is fine.

The sheet uses 15 rows, and every row below this point has been hidden. However, on deleting a complete row, each following row shifts upwards as is standard, leaving a ‘new’, blank row 15. I would like this behaviour to be overruled and for a blank row to not appear.

Might be simple? What would be the option to prevent this behaviour, and if this is impossible are there any tips for grabbing the identity of the ‘last visible’ row of a sheet so as to hide it away manually?

Cheers

How to&Answers:

You may need to adjust this to your needs, but this should work.

Sub GetLastVisibleCell()

Dim rng As Range

Set rng = ActiveSheet.Columns(1).SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeLastCell)

If rng = vbNullString Then rng.EntireRow.Hidden = True


End Sub

Answer:

Instead of deleting a row, clear it instead:

msdn gives an example of

Worksheets("Sheet1").Range("A1:G37").ClearContents

Answer:

The last visible row can also be returned with an end(xlup) syntax. Assuming your column A has data for each non-empty row.

Sub GetLastVisibleCell()

Dim rng As Range

Set rng = cells(rows.count,1).end(xlup)
rng.EntireRow.Hidden = True


End Sub