Cell clear function will not work with hidden rows

dshervin1

New Member
Joined
Jan 4, 2013
Messages
4
I have the following VBA code in my sheet:

Sub Clear_Contents()


Range("b12:b45,b51:b78,b84:b92,b97:b105,d12:d45,d51:d78,d84:d92,d97:d105,e12:e45,e51:e78,e84:e92,e97:e105,f12:f45,f51:f78,f84:f92,f97:f105,g12:g45,g51:g78,g84:g92,g97:g105,h12:h45,h51:h78,h84:h92,h97:h105,i12:i45,i51:i78,i84:i92,i97:i105,k12:k45,k51:k78,k84:k92,k97:k105,m12:m45,m51:m78,m84:m92,m97:m105,n12:n45,n51:n78,n84:n92,n97:n105,o12:eek:45,o51:eek:78,o84:eek:92,o97:eek:105,p12:p45,p51:p78,p84:p92,p97:p105,q12:q45,q51:q78,q84:q92,q97:q105,r12:r45,r51:r78,r84:r92,r97:r105,t12:t45,t51:t78,t84:t92,t97:t105").ClearContents


End Sub

I am getting an error code 400, and I believe it is because some of the cells included in this mixture are in hidden rows, Is there anyway to fix this so I can run this and it will still clear all the cells? If the rows are hidden, then the cells do not have to be cleared if this helps at all.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
<code style="margin: 0px; padding: 0px; font-size: 1em; font-family: Monaco, 'Courier New', monospace;">Hi Dshervin try this one

Sub Clear_Contents()


<code style="margin: 0px; padding: 0px; font-size: 1em; font-family: Monaco, 'Courier New', monospace;">Dim RangesToDelete As String
RangesToDelete =(b12:b45,b51:b78,b84:b92,b97:b105,d12:d45,d51:d78,d84:d92,d97:d105,e12:e45,e51:e78,e84:e92,e97:e105,f12:f45,f51:f78,f84:f 92,f97:f105,g12:g45,g51:g78,g84:g92,g97:g105,h12:h45,h51:h78,h84:h92,h97:h105,i12:i45,i51:i78,i84:i92,i97:i105,k12:k45,k 51:k78,k84:k92,k97:k105,m12:m45,m51:m78,m84:m92,m97:m105,n12:n45,n51:n78,n84:n92,n97:n105,o12:eek:45,o51:eek:78,o84:eek:92,o97:eek:1 05,p12:p45,p51:p78,p84:p92,p97:p105,q12:q45,q51:q78,q84:q92,q97:q105,r12:r45,r51:r78,r84:r92,r97:r105,t12:t45,t51:t78,t8 4:t92,t97:t105)

For Each ws In ThisWorkbook.Worksheets

With ws .AutoFilterMode = False
.Range(RangesToDelete).EntireRow.Hidden = False .Range(RangesToDelete).EntireColumn.Hidden = False
.Range(RangesToDelete).ClearContents 'This deletes only cell contents
'To deletes cell contents and formatting
'.Range(RangesToDelete).Clear
End With
Next ws</code>

End Sub</code>
 
Last edited:
Upvote 0
It still says "Compile Error: Syntax Error." Then it highlights this range: RangesToDelete =(b12:b45,b51:b78,b84:b92,b97:b105,d12:d45,d51:d78,d84:d92,d97:d105,e12:e45,e51:e78,e84:e92,e97:e105,f12:f45,f51:f78,f84:f 92,f97:f105,g12:g45,g51:g78,g84:g92,g97:g105,h12:h45,h51:h78,h84:h92,h97:h105,i12:i45,i51:i78,i84:i92,i97:i105,k12:k45,k 51:k78,k84:k92,k97:k105,m12:m45,m51:m78,m84:m92,m97:m105,n12:n45,n51:n78,n84:n92,n97:n105,o12:eek:45,o51:eek:78,o84:eek:92,o97:eek:1 05,p12:p45,p51:p78,p84:p92,p97:p105,q12:q45,q51:q78,q84:q92,q97:q105,r12:r45,r51:r78,r84:r92,r97:r105,t12:t45,t51:t78,t8 4:t92,t97:t105)
 
Last edited:
Upvote 0
I suspect a typo in that monster of a cell address.
This works for me
Code:
With Range("12:45, 51:78, 84:92, 97:105")
    With Application.Intersect(.EntireRow, Range("B:B, D:I, K:R, T:T"))
        .ClearContents
    End With
End With
 
Upvote 0
Thanks so much to both of you!!! mikeerickson's worked perfectly! I think there was an error in that monster of a cell address, didnt know that you could shorten it like that!!!
 
Upvote 0
Hi Dshervin try this one sorry for the typo.

Sub Clear_Contents()


<code style="margin: 0px; padding: 0px; font-family: Monaco, 'Courier New', monospace; line-height: 13px; color: rgb(51, 51, 51); font-size: 1em; background-color: rgb(250, 250, 250);">Dim RangesToDelete As String
RangesToDelete =("b12:b45,b51:b78,b84:b92,b97:b105,d12:d45,d51:d78,d84:d92,d97:d105,e12:e45,e51:e78,e84:e92,e97:e105,f12:f45,f51:f78,f84:f 92,f97:f105,g12:g45,g51:g78,g84:g92,g97:g105,h12:h45,h51:h78,h84:h92,h97:h105,i12:i45,i51:i78,i84:i92,i97:i105,k12:k45,k 51:k78,k84:k92,k97:k105,m12:m45,m51:m78,m84:m92,m97:m105,n12:n45,n51:n78,n84:n92,n97:n105,o12:eek:45,o51:eek:78,o84:eek:92,o97:eek:1 05,p12:p45,p51:p78,p84:p92,p97:p105,q12:q45,q51:q78,q84:q92,q97:q105,r12:r45,r51:r78,r84:r92,r97:r105,t12:t45,t51:t78,t8 4:t92,t97:t105")

For Each ws In ThisWorkbook.Worksheets

With ws .AutoFilterMode = False
.Range(RangesToDelete).EntireRow.Hidden = False .Range(RangesToDelete).EntireColumn.Hidden = False
.Range(RangesToDelete).ClearContents 'This deletes only cell contents
'To deletes cell contents and formatting
'.Range(RangesToDelete).Clear
End With
Next ws</code>

End Sub

I do not check your ranges and the problem must lie there is the code worked for me. Sorry I had typo problems just now. No quote for my ranges "".

I suggest you check it.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,668
Members
449,463
Latest member
Jojomen56

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