Link to home
Start Free TrialLog in
Avatar of ssblue
ssblueFlag for United States of America

asked on

Excel - click on a cell and have everything in another cell clear

I would like to click on ' J1 ' / or a button and have everything  in H2:H500 be deleted
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

place this in the click event of a button

sheets(1).Range("H2:H500").clearcontents
Place this in Sheet1's code.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("J1")) Is Nothing Then
    Range("H2:H500").ClearContents
End If
End Sub

Open in new window

Avatar of ssblue

ASKER

I'm getting reference not valid.  My sheet name is ' Calculations ' ????  I've never done this so please be specific.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ramin
Ramin

You can record a Macro to do that very easy.
Avatar of ssblue

ASKER

Still getting error - see attachment
test.xlsx
I've attached a sample workbook. When it's open type Alt+F11or click the "Visual Basic" button under "File" to get to Visual Basic. Then in the Project-VBA Project window select "Sheet1(Calculations)"
29002867.xlsm
test this, clikc the button
test.xlsm
Right Click on that button and view code and paste below code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("Calculations")
ws.Range("H2:H500").ClearContents

End Sub

Open in new window

since the button is on sheet Calculations, you only need this line

Range("H2:H500").ClearContents
This Works too !
Only Enable Content,
Microsoft-Excel-Worksheet--3-.xlsm
Avatar of ssblue

ASKER

Thanks for all the suggestions.

However, just for the record, as a novice more explicit directions would have prevented some of the confusion and need for all the extra suggestions.

I granted points based on my original question and the first response that works, even though it took awhile for me to understand what was needed to  make it work.

Thanks again to everyone.
You are most Welcome.