Clearing content for only specific columns with VBA.

sudhansu121

New Member
Joined
Jan 19, 2021
Messages
28
Office Version
  1. 365
Hi Experts,
I have a sheet with a long range (B5:KW14) and each time I have to clear the data manually.
So I have to clear content from B5:H14 and the leave 2 column and aging Start clearing data from L5:L14 so on up to KP5:KW14.
The twist here is after B5:H14, I do not want to clean Column I,J,K and then again start cleaning from L5:L14. I am actually stuck here. I have prepared a macro that clean from B5:KW14.

So may I kindly ask you to help me here?

VBA Code:
Sub CleardatafirstPart()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Select Case ws.Name
 Case "DATOS", "RESUMEN DÍA"
 ' do nothing
 Case Else
 ws.Range("B5:KW14").ClearContents
 ws.Range("B24:KW33").ClearContents
 End Select
Next ws
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi & welcome to MrExcel.
How about
VBA Code:
 ws.Range("B5:H14,L5:KW14").ClearContents
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
ws.Range("B5:H14,L5:KW14").ClearContents
Hi Fluff,
Thanks for supporting me.
The problem is I have to do this manually then. After each 7 columns I have to leave 3 columns untouched.
now with your code it will clear fromB5 to H14 and leave ,I,J,K untouched. But if we use the code L5:KW15, then it will clear the data from S,T,U column and so on.

So I want something so that It can automatically take 7 column and clear data and then leave 3 column untouched until last column
 
Upvote 0
I'm sorry but I don't understand.
Will the columns to be cleared always be the same?
 
Upvote 0
I'm sorry but I don't understand.
Will the columns to be cleared always be the same?
Yes the columns to be cleared will always be same.
To elaborate,
For example I have 20 columns in Excel.
I want to clear data for each 7 columns and then next 3 coulmns would remain same and data should not be cleaned.

So column 1, to 7- Clear Content
8,9 and 10 No changes needed.
Then again Column 11 to 17 -clear content
18,19 and 20 No changes needed.

So for my excel File the logic is same as above it just should be automated and consider up to last column
 
Upvote 0
In that case you can do that like
VBA Code:
 ws.Range("B5:H14,L5:R14,V5:AB14,AF5:AL14").ClearContents
 
Upvote 0
In that case you can do that like
VBA Code:
ws.Range("B5:H14,L5:R14,V5:AB14,AF5:AL14").ClearContents
Yes Fluff this is exactly what I want :) Is there a way to do it Automatically? So I do not have to write the whole thing ?
 
Upvote 0
Maybe...
VBA Code:
Sub sudhansu121()
    Dim i As Long
    For i = 2 To Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column Step 10
        Intersect(Columns(i).Resize(, 7), Rows("5:14")).ClearContents
    Next
End Sub
 
Upvote 0
Solution
A
Maybe...
VBA Code:
Sub sudhansu121()
    Dim i As Long
    For i = 2 To Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column Step 10
        Intersect(Columns(i).Resize(, 7), Rows("5:14")).ClearContents
    Next
End Sub
Allow me 30 Mins and will update you :)
 
Upvote 0
A

Allow me 30 Mins and will update you :)
Great Fluff this is working perfect.
I just found out one more problem and we need t modify the code.

After the 1st column we should not change the data at column 2,12 and so on.

I want to clear data for each 7 columns and then next 3 columns would remain same and data should not be cleaned.

So column 1, to 7- Clear Content
2,8,9 and 10 No changes needed.
Then again Column 11 to 17 -clear content
12,18,19 and 20 No changes needed.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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