+ Reply to Thread
Results 1 to 3 of 3

Limit Shift Cells to Left to a Certain Range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Limit Shift Cells to Left to a Certain Range

    Hi all

    My file has one record per line.
    In Range BI:BS it may happen that the user deletes a certain value.
    The macro that will run should rearrange the values, so that there are no blank cells between values.
    However, values after column BS should remain where they are.

    234 123 345 456

    should be re-arranged to

    234 123 345 456

    Important, the order of the values itself should not be changed.
    i.e. it should NOT look like this

    123 245 345 456

    How do I do that?

    Thanks FD

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Limit Shift Cells to Left to a Certain Range

    In the worksheet Class module, and very simplistically...
    Private Sub Worksheet_Change(ByVal Target As Range)
       
       Dim lngCol As Long
       Dim lngCell As Long
       Dim c As Excel.Range
       
       On Error GoTo Catch
       
       Const MinCol As Long = 61
       Const MaxCol As Long = 71
       
       If Target.Areas.Count > 1 Then
          MsgBox "Cannot process non-contiguous ranges...", vbExclamation, "Error"
          Exit Sub
       Else
          Application.EnableEvents = False
       End If
       
       '// Work from Last Cell to first...
       For lngCell = Target.Cells.Count To 1 Step -1
    
          Set c = Target.Cells(lngCell)
    
          If c.Value = vbNullString And c.Column >= MinCol And c.Column <= MaxCol Then
    
             For lngCol = 0 To (MaxCol - 1) - c.Column
                c.Offset(, lngCol).Value = c.Offset(, lngCol + 1).Value
             Next
          
             '// Something was deleted, the LAST cell, at least, must be blank
             c.Offset(, MaxCol - c.Column).Value = vbNullString
       
          End If
    
       Next
       
    Catch:
       
       '// not interested in errors - just turn on Event handling
       Application.EnableEvents = True
       
    End Sub
    Changed to work with multiple cells, but not multiple areas. The selection must be contiguous...
    Last edited by cytop; 01-14-2015 at 07:43 AM. Reason: Changed to work with multiple cells.

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

    Re: Limit Shift Cells to Left to a Certain Range

    Maybe:

    Sub FallingDown()
    Dim i As Long
    Dim y As Long
    For i = 2 To Range("BT" & Rows.Count).End(3).Row
        For y = 61 To 70
            If Cells(i, y) = "" Then
            Cells(i, "BS").Insert xlToRight
            Cells(i, y).Delete xlToLeft
            End If
        Next y
    Next i
    End Sub

+ 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. go through a range and delete, shift cells left if contain no data.
    By 13lack13lade in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2014, 09:50 PM
  2. [SOLVED] If criteria met: delete cell and shift to the left but shift only over a certain range
    By olivierpbeland in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-11-2013, 07:44 AM
  3. Replies: 0
    Last Post: 03-26-2013, 02:58 PM
  4. shift range of cells to left if find the range of empty do while
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2012, 09:25 AM
  5. Move/Shift cells left or right in a row
    By T.Taylor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-14-2005, 11:20 AM

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