clear cells with vba based on selection

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I have a code that I am using to clear cell contents and formatting, however what I need to do is also clear the contents of whatever is in Column B of the same row(s) that are selected and having the contents deleted. Here's my code:


Code:
Sub Clear()With Selection
    .Interior.Color = xlNone
    .Font.ColorIndex = xlAutomatic
End With
Sheets("RO").Visible = True
Sheets("RO").Range(Selection.Address).Offset(-85, -2).ClearContents
'Sheets("RO").Visible = False
Sheets("Scheduler").Select
End Sub

So, if cells F89:I89 were selected, then the contents of B89 would be cleared.
If G90:K92 were selected then B90:B92 would need to be cleared.

Is this doable?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Is this doable?

Yes - try this:

Code:
Option Explicit
Sub Clear()

    Dim rngMyRow As Range
    
    Application.ScreenUpdating = False
    
    For Each rngMyRow In Selection.Rows
        Range("B" & rngMyRow.Row).ClearContents
    Next rngMyRow

    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
.. or clear them all at once:
Code:
Intersect(Selection.EntireRow, Columns("B")).ClearContents


I would also suggest not using a vba keyword (Clear) as as sub name.
 
Last edited:
Upvote 0
Hi Andrew,

You're welcome :)

Make sure you also try Peter's solution as it looks pretty nifty!!

Regards,

Robert
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

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