Used Range in Vba taking all cells not just used cells

dsrt16

Board Regular
Joined
Jun 18, 2005
Messages
208
I have a code in vba to clear all unlocked cells in the used range on every sheet.

It takes 40 minutes to execute this code. In order to figure out why it was taking so long, I used vba to select the used range.

I do not use any columns past column S. But currently, it has all the way to column iv as part of the used range. Same thing with the rows. It selects far more than the actual used rows. A user will never go past column S, but as users add new rows to tables, it is hard to determine how many rows they will use.

Anyway, before building my excel tables, I first selected all cells and changed the fill to white (thus getting rid of the excel gray lines). Since I did that, I think that is why it is counting it as part of the used range.

Is there any way to get this code to just check the actual used range. The cells with data in them not just formatting?

Code:
Sub NewYear()
Dim cell As Range, Sht As Worksheet


For Each Sht In ThisWorkbook.Worksheets
   If Sht.Name <> "Setup Page" And Sht.Name <> "Lists" Then
    For Each cell In Sht.UsedRange
    If cell.Locked = False Then cell.Value = ""
    Next cell
    End If
 Next Sht

 Call UnprotectRefresh

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The best way would be to delete all the columns from T to IV. Remove the background, & turn grid lines off.
Then save & close your workbook. Once you re-open it your macro should work.
 
Upvote 0
The best way would be to delete all the columns from T to IV. Remove the background, & turn grid lines off.
Then save & close your workbook. Once you re-open it your macro should work.
Maybe reset the remaining cells' fill color to xlNone as well? I see no reason to keep the white fill color once the grid lines are turned off.
 
Upvote 0
Maybe reset the remaining cells' fill color to xlNone as well? I see no reason to keep the white fill color once the grid lines are turned off.
That's what I meant. Maybe I should have said - Remove the background fill from the entire sheet.
 
Last edited:
Upvote 0
All this time, and I never knew you could turn the gridlines off. Mind blown! Haha.

Ok thanks. Easy fix!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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