Results 1 to 9 of 9

Thread: How to arrange cells like this?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member
    Join Date
    May 2013
    Posts
    31
    Rep Power
    0
    Place code in a Module. Run for active sheet. Be sure to run on a backup copy as usual when testing copy.

    Code:
    Sub MoveRowsUp()
      Dim r As Range, r1 As Range, r2 As Range, v, i As Long
      
      Set r = Range("A1", Range("A1").End(xlDown).Offset(1))
      
      Application.EnableEvents = False
      Application.DisplayAlerts = False
      Application.Calculation = xlCalculationManual
      Application.ScreenUpdating = False
      
      On Error GoTo EndSub
      For i = r.Rows.Count To 1 Step -1
        Set r1 = Cells(i, "A")
        Set r2 = Cells(i - 1, "A")
        If r1.Value = "" And r2.Value > 0 Then
          'Concatenate column B values
          v = r2.Offset(, 1).Value & r1.Offset(, 1).Value
          Range("B" & i & ":F" & i).Cut Destination:=Range("B" & i - 1 & ":F" & i)
          Rows(i).Delete Shift:=xlUp
          Cells(i - 1, "B").Value = v 'Add concatenated column B value back.
        End If
      Next i
      
    EndSub:
      Application.EnableEvents = True
      Application.DisplayAlerts = True
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
    End Sub

  2. #2
    Junior Member
    Join Date
    Jul 2017
    Posts
    3
    Rep Power
    0
    Quote Originally Posted by Kenneth Hobson View Post
    Place code in a Module. Run for active sheet. Be sure to run on a backup copy as usual when testing copy.

    Code:
    Sub MoveRowsUp()
      Dim r As Range, r1 As Range, r2 As Range, v, i As Long
      
      Set r = Range("A1", Range("A1").End(xlDown).Offset(1))
      
      Application.EnableEvents = False
      Application.DisplayAlerts = False
      Application.Calculation = xlCalculationManual
      Application.ScreenUpdating = False
      
      On Error GoTo EndSub
      For i = r.Rows.Count To 1 Step -1
        Set r1 = Cells(i, "A")
        Set r2 = Cells(i - 1, "A")
        If r1.Value = "" And r2.Value > 0 Then
          'Concatenate column B values
          v = r2.Offset(, 1).Value & r1.Offset(, 1).Value
          Range("B" & i & ":F" & i).Cut Destination:=Range("B" & i - 1 & ":F" & i)
          Rows(i).Delete Shift:=xlUp
          Cells(i - 1, "B").Value = v 'Add concatenated column B value back.
        End If
      Next i
      
    EndSub:
      Application.EnableEvents = True
      Application.DisplayAlerts = True
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
    End Sub
    Works but only once for 1 row. Should do the whole sheet in a single go.
    Also the column F becomes blank (Cr/Dr goes away)

Similar Threads

  1. Replies: 4
    Last Post: 08-20-2013, 06:28 PM
  2. Replies: 13
    Last Post: 06-10-2013, 09:05 AM
  3. Replies: 2
    Last Post: 09-24-2012, 11:19 PM
  4. Arrange the data by year and format.
    By pesteness in forum Excel Help
    Replies: 15
    Last Post: 08-19-2012, 08:54 PM

Posting Permissions

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