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
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,743
Office Version
2013
Platform
Windows
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,673
Office Version
2019
Platform
Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,743
Office Version
2013
Platform
Windows
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.
 

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,743
Office Version
2013
Platform
Windows
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:
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,095,180
Messages
5,442,855
Members
405,203
Latest member
Mira_Xcel

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top