PDA

View Full Version : VBA clearing cells based on a selection



orca
07-02-2015, 07:41 AM
Based on a serial number entered at or below cell C8, a macro runs and prints results in columns D through G to the right of the input cell. The macro only runs if the active cell is at or below C8, so I want similar cell restrictions for the reset button.

I was able to write a code that clears all information from columns C through G, but the user must clear rows one at a time. I want the user to select multiple cells in column C and clear all cells to the right of those. Additionally, the cells may initially be colored, so I need to include .Interior.Color = xlNone

Since the user selection is unknown, I have been trouble defining a range. The selected range may be C8, or C10 through C20, or any other combination of cells.

Any help would be great.

SamT
07-02-2015, 10:37 AM
Lets see the code that you have. with the cursor in the code page, press Ctrl+A, then Ctrl+C. Then, in the Post editor menu, Click the # Icon, then Press Ctrl+V.

orca
07-03-2015, 02:43 PM
I figured it out. I was looking for the lines Dim c As Range and For Each c In Selection

Sub clear()
Dim c As Range
For Each c In Selection
c.Offset(0, 1) = blank
c.Offset(0, 2) = blank
c.Offset(0, 3) = blank
c.Offset(0, 4) = blank
Next c
End Sub

SamT
07-03-2015, 09:13 PM
Sub clear()
Selection.Resize(1, 4). ClearContents
End sub