+ Reply to Thread
Results 1 to 6 of 6

Dynamic Range for clear cells on change

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2013
    Location
    Jupiter FL
    MS-Off Ver
    Excel 2010
    Posts
    6

    Exclamation Dynamic Range for clear cells on change

    I am working on a "SUPER RUSH--GET IT DONE LAST WEEK" project that requires a dynamic range of rows. There are dependent data validation drop down lists involved. I want to be able to clear cells when one of the drop down values changes.

    For example: User originally chose "Apples" from drop down list in Column E, then "Granny Smith" from drop down list in Column F, but wants to make it "Oranges", and "Valencia", or perhaps just clear that row and not make any more choices at this time.

    Based on their choice of Granny Smith Apples, a price populates automatically in Column G using INDEX MATCH MATCH formula. When the user changes their mind, I want to clear Column E and F, thereby clearing Column G automatically (but NOT clear the formula!). The user could clear Column E by deleting contents in Column E, or just making another choice.

    I have found code online that works for a static range, but only if user makes an alternate choice. I need to make it a dynamic range (not just "E22:E32"), AND/OR clear contents if user deletes previous contents.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim sValue As String
        
        If Intersect(Target, Range("E22:E32")) Is Nothing Or _
            Target.Count > 1 Or _
            Target.Text = vbNullString Then Exit Sub
            
        On Error GoTo CleanUp
        Application.EnableEvents = False
        
        sValue = Target.Text
        Intersect(Target.EntireRow, Columns("F:F")).ClearContents
        Target = sValue
       
    CleanUp:
        Application.EnableEvents = True
    End Sub
    I have not worked with vba for many years, so I am really having problems with something that should be relatively simple. Any help would be VERY APPRECIATED!!!!

  2. #2
    Forum Contributor
    Join Date
    07-24-2009
    Location
    Valrico, FL USA
    MS-Off Ver
    Excel 2016
    Posts
    358

    Re: Dynamic Range for clear cells on change

    This may help

    The macro below will clear the cell contents below row 1.
    Columns B and C of the target row will be cleared if column A changes.
    Column C of the target row will be cleared if column B changes.

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Row = 1 Then Exit Sub
      If Target.Column > 2 Then Exit Sub
      On Error GoTo ErrHandler
      Application.EnableEvents = False
      Select Case Target.Column
        Case 1
           Target.Offset(0, 1).Value = ""
           Target.Offset(0, 2).Value = ""
        Case 2
           Target.Offset(0, 1).Value = ""
      End Select
    ErrHandler:
      Application.EnableEvents = True
    End Sub

  3. #3
    Registered User
    Join Date
    10-01-2013
    Location
    Jupiter FL
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Dynamic Range for clear cells on change

    I appreciate your response, however, it doesn't seem to work. I replaced my original code with what you provided, and made the following changes:

    If Target.Row = 22 Then Exit Sub  'all changes will be row 23 or below.
      If Target.Column > 6 Then Exit Sub  'user initiated changes will be in columns E:F.
    This results in no changes at all, except the ones I initiate. Cells don't clear at all. Am I missing something? Am I being completely dense? (Wouldn't surprise me a bit right now -brain drain to the max...LOL)

  4. #4
    Forum Contributor
    Join Date
    07-24-2009
    Location
    Valrico, FL USA
    MS-Off Ver
    Excel 2016
    Posts
    358

    Re: Dynamic Range for clear cells on change

    Try the following code

    Private Sub Worksheet_Change(ByVal Target As Range)
      
      If Target.Row < 23 Then Exit Sub  'all changes will be row 23 or below.
      If Target.Column = 5 Then ' If changes are made in column E the adjacent cell in column F will be cleared
      
      On Error GoTo ErrHandler
      Application.EnableEvents = False
           Target.Offset(0, 1).Value = ""
      End If
    ErrHandler:
      Application.EnableEvents = True
    End Sub

  5. #5
    Registered User
    Join Date
    10-01-2013
    Location
    Jupiter FL
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Dynamic Range for clear cells on change

    That seems to work perfectly! Thank you!!
    Now, my boss is considering a change to the format of the project. If he goes with what he is thinking, I will need to have this stop at a certain place, but the row number will still be variable. How is the best way to do that? For example: same as above, only now he wants to add a Grand Total row, and have a totally different set of info below that. I will need the text clearing code to stop at the row above the Grand Total row so we don't have problems with the cells below. Sorry to change it up on you, but the boss is the boss!

  6. #6
    Forum Contributor
    Join Date
    07-24-2009
    Location
    Valrico, FL USA
    MS-Off Ver
    Excel 2016
    Posts
    358

    Re: Dynamic Range for clear cells on change

    Uploading a sample workbook would be helpful

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Clear cells upon adjacent cell change
    By jwstimac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2013, 01:02 PM
  2. [SOLVED] Macro needed to clear cells in range based on value of cells in another range
    By msmith7113 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2013, 12:32 AM
  3. Using an event change to clear cells in worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 04-15-2012, 08:34 AM
  4. Clear cells using a worksheet change event
    By headexperiment in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2011, 07:26 AM
  5. Workshet Change Event for dynamic range starting but not firing for target cells
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-06-2010, 08:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1