+ Reply to Thread
Results 1 to 7 of 7

If criteria met: delete cell and shift to the left but shift only over a certain range

Hybrid View

  1. #1
    Registered User
    Join Date
    07-24-2011
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    91

    If criteria met: delete cell and shift to the left but shift only over a certain range

    Hi, not sure how clearly I can explain this but please look at the worksheet attached for a clearer explanation.

    I have 9 Columns (C,D,E,F,G,H,I,J,K) with text in it. The number of rows varies so the code must look for the last non-empty row in column C.

    I am looking for a code to look for a particular value in range D6:H? (e.g. "Name Whatever" located in a "Target" cell in the same worksheet).

    If a cell in range D6:H? contains the target, the cell must be deleted and the cells must be shifted to the left but only over the range D:H.

    For instance, suppose cell E10 contains the target, then the content of cell E10 would be replaced by that of cell F10, the content of cell F10 would be replaced by the content of cell G10, the content of cell G10 would be replaced by the content of cell H10 and the cell H10 would be blank. Importantly, the cells in C10 and I10:K10 would not be modified.

    This means that if a match is found in cell H?, it simply gets replaced by an empty cell (no cell to the left to shift).

    As I said, this is easier to understand by taking a look at the attached worksheet.

    Many thanks for the help!

    Example2.xls

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If criteria met: delete cell and shift to the left but shift only over a certain range

    Maybe:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("D2:H" & Range("C" & Rows.Count).End(3)(2).Row)) Is Nothing Then
        Select Case Target.Column
            Case Is = 4
                If Target.Text = "Name Whatever" Then
                    Target.Clear
                    Range(Target.Offset(, 1), Target.Offset(, 4)).Cut Target
                End If
            Case Is = 5
                If Target.Text = "Name Whatever" Then
                    Target.Clear
                    Range(Target.Offset(, 1), Target.Offset(, 3)).Cut Target
                End If
            Case Is = 6
                If Target.Text = "Name Whatever" Then
                    Target.Clear
                    Range(Target.Offset(, 1), Target.Offset(, 2)).Cut Target
                End If
            Case Is = 7
                If Target.Text = "Name Whatever" Then
                    Target.Clear
                    Target.Offset(, 1).Cut Target
                End If
            Case Is = 8
                If Target.Text = "Name Whatever" Then
                    Target.Clear
                End If
        End Select
    End If
            
            
                    
    End Sub

  3. #3
    Registered User
    Join Date
    07-24-2011
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    91

    Re: If criteria met: delete cell and shift to the left but shift only over a certain range

    Maybe my choice of word was confusing. What I call "target" is just the cell where the name to look for "Name Whatever" is located in my worksheet. It's not the destination of anything. Makes any sense?

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If criteria met: delete cell and shift to the left but shift only over a certain range

    The code is a worksheet change event. Target in this case means if you select a cell in the designated range (ie. D2-H & lastrow) then that cell is the Target. If you put in "Name Whatever" in the Target cell then the shift you desired should take place. Sorry, if I misunderstood what you want to happen.

  5. #5
    Registered User
    Join Date
    07-24-2011
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    91

    Re: If criteria met: delete cell and shift to the left but shift only over a certain range

    Thanks. So the Sub takes "target" as an argument? Not sure what this range refers to in your code?

  6. #6
    Registered User
    Join Date
    07-24-2011
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    91

    Re: If criteria met: delete cell and shift to the left but shift only over a certain range

    Ok. I have managed to get it to work perfectly. Thank you so much.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If criteria met: delete cell and shift to the left but shift only over a certain range

    You're welcome. Glad to help out, and thanks for the feedback.

+ 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. Delete empty cells, and shift left
    By kiboodez in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 07-25-2014, 07:53 AM
  2. [SOLVED] Conditional Delete Cells and shift to the left
    By markos97 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 07-19-2013, 12:32 PM
  3. Replies: 0
    Last Post: 03-26-2013, 02:58 PM
  4. delete blank cells and shift data left
    By rhudgins in forum Excel General
    Replies: 7
    Last Post: 07-30-2010, 11:16 PM
  5. Delete empty cells and shift remaining left
    By kiboodez in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2010, 02:17 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