+ Reply to Thread
Results 1 to 3 of 3

Help with Code - clearing contents of cells

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    42

    Unhappy Help with Code - clearing contents of cells

    Hello excel universe gods,

    I was able to sucessfully create a code that would erase the contents of other cells if they did not match up with the original one (i.e A2).

    Issue
    I wanted to expand this code to clear the content of more cells. Currently, it only work on A, B, and C. Do you know how I can expand it to D, E, and F

    This code only works for the second row, but i need it to encompass the 2nd, 3rd, and 4th row. Any help would be awesome!

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        
    Dim rngState As Range
        Dim rngDivision 
    As Range
        Dim rngClear 
    As Range
        Dim rCell 
    As Range
        
        Set rngState 
    Intersect(Columns("A"), Target)
        
    Set rngDivision Intersect(Columns("B"), Target)
        
        
    Application.EnableEvents False
        
        
    If Not rngState Is Nothing Then
            
    For Each rCell In rngState.Cells
                Select 
    Case (rngClear Is Nothing)
                    Case 
    True:  Set rngClear rCell.Offset(, 1).Resize(, 2)
                    Case Else:  
    Set rngClear Union(rngClearrCell.Offset(, 1).Resize(, 2))
                
    End Select
            Next rCell
            rngClear
    .ClearContents
            Set rngClear 
    Nothing
        End 
    If
        
        If 
    Not rngDivision Is Nothing Then
            
    For Each rCell In rngDivision.Cells
                Select 
    Case (rngClear Is Nothing)
                    Case 
    True:  Set rngClear rCell.Offset(, 1)
                    Case Else:  
    Set rngClear Union(rngClearrCell.Offset(, 1))
                
    End Select
            Next rCell
            rngClear
    .ClearContents
            Set rngClear 
    Nothing
        End 
    If
        
        
    Application.EnableEvents True
        
    End Sub 
    I'm sure this is an easy fix. Thank you!

    Sajan

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help with Code - clearing contents of cells

    First guess... change .Resize(, 2) to .Resize(, 3)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: Help with Code - clearing contents of cells

    Wow,

    JBeaucaire, u are a genuis! I changed it Resize (, 5) and it works perfectly now.

    Thank you very much!

+ 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. VBA code to clear the contents (or value) of a cell without clearing the formula
    By BazzaBit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-10-2013, 08:16 PM
  2. [SOLVED] Clearing the contents of certain cells after another cell has been changed.
    By ndrobinson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-13-2012, 11:14 AM
  3. [SOLVED] Clearing contents of only blank cells
    By dwhite30518 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-29-2012, 09:35 AM
  4. [SOLVED] [SOLVED] Clearing the Contents of Merged Cells
    By prizm1 in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 09-10-2005, 12:05 AM
  5. clearing the contents of specified cells
    By Jimmy Pop in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-26-2005, 09:05 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