Simplify VB Code

Status
Not open for further replies.

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
Dear Sir
Can you help to simplify below code since it is making excel sheet slow

Sub sbClearCellsOnly()


Range("B6:P6").ClearContents
Range("B10:P10").ClearContents
Range("B14:P14").ClearContents
Range("B18:P18").ClearContents
Range("B22:P22").ClearContents
Range("B26:P26").ClearContents
Range("B30:P30").ClearContents
Range("B34:P34").ClearContents
Range("B38:P38").ClearContents
Range("B42:P42").ClearContents
Range("B46:P46").ClearContents
Range("B50:P50").ClearContents
Range("B54:P54").ClearContents
Range("B58:P58").ClearContents
Range("B62:P62").ClearContents
Range("B66:P66").ClearContents
Range("B70:P70").ClearContents
Range("B74:P74").ClearContents
Range("B78:P78").ClearContents
Range("B82:P82").ClearContents
Range("B86:P86").ClearContents
Range("B90:P90").ClearContents
Range("B94:P94").ClearContents
Range("B98:P98").ClearContents
Range("B102:P102").ClearContents
Range("B106:P106").ClearContents
Range("B110:P110").ClearContents
Range("B114:P114").ClearContents
Range("B118:P118").ClearContents
Range("B122:P122").ClearContents
Range("B126:P126").ClearContents
Range("B130:P130").ClearContents
Range("B134:P134").ClearContents
Range("B138:P138").ClearContents
Range("B142:P142").ClearContents
Range("B146:P146").ClearContents
Range("B150:P150").ClearContents
Range("B154:P154").ClearContents
Range("B158:P158").ClearContents
Range("B162:P162").ClearContents
Range("B166:P166").ClearContents
Range("B170:P170").ClearContents
Range("B174:P174").ClearContents
Range("B178:P178").ClearContents
Range("B182:P182").ClearContents
Range("B186:P186").ClearContents
Range("B190:P190").ClearContents
Range("B194:P194").ClearContents
Range("B198:P198").ClearContents
Range("B202:P202").ClearContents
Range("B206:P206").ClearContents
Range("B210:P210").ClearContents
Range("B214:P214").ClearContents
Range("B218:P218").ClearContents
Range("B222:P222").ClearContents
Range("B226:P226").ClearContents
Range("B230:P230").ClearContents
Range("B234:P234").ClearContents
Range("B238:P238").ClearContents
Range("B242:P242").ClearContents
Range("B246:P246").ClearContents
Range("B250:P250").ClearContents


'
Range("B9:B250").Select
Range("B250").Activate
Selection.EntireRow.Hidden = True
Range("B6").Select






End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:
Code:
Sub Clear_Me()
'Modified 12/29/2018 2:34:32 AM  EST
Application.ScreenUpdating = False
Dim i As Long
For i = 6 To 250 Step 4
    Cells(i, 2).Resize(, 15).ClearContents
Next
Range("B9:B250").Select
Range("B250").Activate
Selection.EntireRow.Hidden = True
Range("B6").Select

Application.ScreenUpdating = True
End Sub
 
Upvote 0
try,

Code:
Sub sbClearCellsOnly()
    Dim r As Long
    For r = 6 To 250 Step 4
        Cells(r, 2).Resize(, 15).ClearContents
    Next r
    Range("B250").EntireRow.Hidden = True
    Range("B6").Select
End Sub

Dave
 
Upvote 0
I never click on links:
If you need more help then show us all the code you have in your sheet and explain in words what your trying to do.
 
Upvote 0
All right sir, see below codes;

This is to unhide rows between A5:A250

Sub addrow()
Dim Ar As Areas
Set Ar = Range("A5:A250").SpecialCells(xlVisible).Areas
Ar(Ar.Count).Offset(4).Resize(2).EntireRow.Hidden = False
End Sub

This is to hide rows between A250:A9 as well as it will clear content to next rows means A250 then A246 so on till A10

Sub deleterow()

Dim Ar As Areas
Set Ar = Range("A5:A250").SpecialCells(xlVisible).Areas
Ar(Ar.Count).Offset(1, 1).Resize(1, 15).ClearContents
Ar(Ar.Count).EntireRow.Hidden = True
End Sub



This is last code which I added to clear contents of selected rows and hide Row 9:250
Sub Clear_Me()
'Modified 12/29/2018 2:34:32 AM EST
Application.ScreenUpdating = False
Dim i As Long
For i = 6 To 250 Step 4
Cells(i, 2).Resize(, 15).ClearContents
Next
Range("B9:B250").Select
Range("B250").Activate
Selection.EntireRow.Hidden = True
Range("B6").Select


Application.ScreenUpdating = True
End Sub
 
Upvote 0
I provided the code you originally asked for and you said it worked perfectly.

Now trying to read other peoples code and sorting out how one piece of code could cause other code to not work is not something I'm good at.

I normally never try to help with hiding and un hiding rows columns sheets and other things. It's a very common question on this forum so I'm sure someone else will be able to help you.
 
Last edited:
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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