Delete Unused Cells

DaveRadford

Board Regular
Joined
Feb 18, 2010
Messages
63
Afternoon,

I have a piece of code that will shrink down the size of my spreadsheet from 12mb to 3mb. This code works but it will not run when worksheets are protected.

Seems to point to:

Else
.Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete
.Range(.Cells(1, myLastCol + 1), _
.Cells(1, .Columns.Count)).EntireColumn.Delete



I have tried placing the following in various points of the code but still fails. I am doing this upon save so it will tidy it up hence why it needs to go back to being protected once its done.

ActiveSheet.Unprotect Password:="password"

and

ActiveSheet.protect Password:="password"



Dim myLastRow As Long
Dim myLastCol As Long
Dim wks As Worksheet
Dim dummyRng As Range

For Each wks In ActiveWorkbook.Worksheets

With wks
myLastRow = 0
myLastCol = 0
Set dummyRng = .UsedRange
On Error Resume Next
myLastRow = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByRows).Row
myLastCol = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByColumns).Column
On Error GoTo 0
If myLastRow * myLastCol = 0 Then
.Columns.Delete

Else
.Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete
.Range(.Cells(1, myLastCol + 1), _
.Cells(1, .Columns.Count)).EntireColumn.Delete
End If

End With

Next wks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Have you tried:

Code:
Dim myLastRow As Long
Dim myLastCol As Long
Dim wks As Worksheet
Dim dummyRng As Range

For Each wks In ActiveWorkbook.Worksheets

With wks
myLastRow = 0
myLastCol = 0
Set dummyRng = .UsedRange
On Error Resume Next
myLastRow = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByRows).Row
myLastCol = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, lookat:=xlWhole, _
searchdirection:=xlPrevious, _
searchorder:=xlByColumns).Column
On Error GoTo 0
If myLastRow * myLastCol = 0 Then
.Columns.Delete

Else
[B][COLOR=red].Unprotect Password:="password"
[/COLOR][/B].Range(.Cells(myLastRow + 1, 1), _
.Cells(.Rows.Count, 1)).EntireRow.Delete
.Range(.Cells(1, myLastCol + 1), _
.Cells(1, .Columns.Count)).EntireColumn.Delete
[B][COLOR=red].Protect Password:="password"
[/COLOR][/B]End If

End With

Next wks
 
Upvote 0
Have you tried using wks instead of ActiveSheet when you are unprotecting the worksheet(s)?
 
Upvote 0
Hello again,

I have used the code above, and it works...ish. The isue is i have 56 worksheets and it systematically goes through each and everyone tidying them up.

Is there a quicker way as saving an take up to 3 minutes a time now, but my main concern.....as it checks each worksheet it actually changes to the view so you are shown each worksheet it is checking making it obvious what its doing.

Is there a way to do it where the worksheet that is displayed stays put?

Thanks,

Dave
 
Upvote 0
I think you mean :-

Code:
Application.ScreenUpdating = False

which will turn off screen updates, and then:-


Code:
Application.ScreenUpdating = True

Thanks

Kaps
 
Upvote 0
Is there a way to do it where the worksheet that is displayed stays put?

You can use:
Application.ScreenUpdating = False
at the start of the routine, and at the end:
Application.ScreenUpdating = True

This is typical good practise to make code more efficient.

Are you aware that the UsedRange property can also be used as a method to reset the size of the used range? I.e., simply putting:

Sheets(1).UsedRange

..will reset the used range.
 
Upvote 0
No i wasnt aware of that, interesting. As you can see from an earlier reply i had it looks to be a serious amount of code in comparison.

If i wanted it to reset the used range on each sheet, could i do soemthing like worbook.usedrange ?

or would it just be easier to do soemthing like:

worksheet(1).usedrange
worksheet(2).usedrange
worksheet(3).usedrange

etc

keeping in mind efficient code i doubt it :)
 
Upvote 0
You're on the right track, but it depends. You can look for the last occupied cell in a sheet and then delete all subsequent columns and rows. That method will also therefore delete rows/columns where they still contain formatting (e.g. fill color) - since your find code doesn't look for the last formatted cell, but rather the last cell that contains a value.

UsedRange will still consider anything that is used, formatting and all.
 
Upvote 0
I was testing this and when i tried to use:

Worksheet(2).UsedRange
Worksheet(3).UsedRange
Worksheet(4).UsedRange
Worksheet(5).UsedRange

(3) = the worksheet number i also tried Worksheet("Name").UsedRange.

Both cam up with sub or Function not defined, im using it in a macro which im calling in the worksheet. Should this be in the spreadsheet?
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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