Clear contents with vba

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hi there,

I am trying to use VBA to clear the contents of column B of the same row(s) as the cells selected. I just can't quite figure out how to get it right.


If D5 was selected then B5 would be cleared.
If F7:G8 was selected then B7:B8 would be cleared.

Thanks,
Andrew
 
A bit of an unusual request but try the code below...

Code:
Sub clearB()
Range("B" & Selection.Row & ":" & "B" & Selection.Row + Selection.Rows.Count - 1).ClearContents
End Sub

please note this will only work for a single range.
 
Last edited:
Upvote 0
^^ Far more succinct than mine! But I've written this now, so may as well post :p

Code:
Sub ClearCells()
Dim rng As Range

If Selection.Count = 1 Then
    Range("B" & ActiveCell.Row).ClearContents
Else
    Set rng = Selection
    Range("B" & rng(1).Row, "B" & rng(rng.Count).Row).ClearContents
End If

End Sub

Thanks
Caleeco
 
Last edited:
Upvote 0
Perhaps:

Code:
Sub test()
Cells(Selection.Row, 2).Resize(Selection.Rows.Count).ClearContents
End Sub
 
Upvote 0
Several ways to skin the cat, oops, sorry...
Code:
Intersect(Columns("B"), Selection.EntireRow).ClearContents
 
Last edited:
Upvote 0
Thank you all for the replies. Kenneth, I used yours (as intersect was what I was actually trying to use before) and it works perfectly!
 
Upvote 0

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