Consulting

Results 1 to 13 of 13

Thread: Solved: Move every Nth cell up without affecting the rest of the columns

  1. #1
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location

    Solved: Move every Nth cell up without affecting the rest of the columns

    Move every Nth cell up without affecting the rest of the columns.


    Hi there,

    I want to move the content of every Nth cell in a column up to just below the previous "N-cell" without affecting the rest of the columns.
    In other words, in a specific column I want to keep for example every fourth entry, remove everything in between and move them up so that all the original fourth entry cell contents are now directly below each other, without affecting the other columns.
    1a => 1a
    2b => 2d
    3c => 3g
    4d => 4j
    5e => 5
    6f => 6
    7g => 7
    8h => 8
    9i => 9
    10j => 10

    Thanks

  2. #2
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Try something like this:

    [VBA]Sub test()
    Dim iNth As Integer
    Dim str As String
    Dim lRow As Long
    Dim lCount As Long
    Dim iCol As Integer
    Dim rng As Range
    Dim sCol As String

    iCol = 2 'change this to adjust the column you wish to fix
    sCol = "B" 'change this to match icol's letter
    iNth = Application.InputBox(Prompt:="Please enter the number you wish to use", Type:=1)
    lCount = 1

    For lRow = 1 To Sheet1.Range(sCol & Sheet1.Rows.Count).End(xlUp).Row
    str = Sheet1.Cells(1 + ((lRow - 1) * iNth), iCol)
    Sheet1.Cells(lRow, iCol) = str
    lCount = lCount + 1
    Next lRow

    Set rng = Sheet1.Range(sCol & lCount & ":" & sCol & Sheet1.Rows.Count)
    rng.ClearContents



    End Sub[/VBA]

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I notice that in your example, you are keeping the value of every third cell.
    Perhaps something like[VBA]Dim FourthCellValues as Variant
    Dim i As Long, Pointer As Long

    With Sheet1.Range("A:A")
    With Range(.Cells(1,1), .Cells(.Rows.Count, 1).End(xlup))
    ReDim FourthCellValues(1 to .Rows.Count)
    For i = 1 To .Rows.Count Step 3
    Pointer = Pointer + 1
    FourthCellValues(Pointer) = .Cells(i,1).Value
    Next i
    ReDim Preserve FourthCellValues(1 to Pointer)
    .Clear.Contents
    .Resize(Pointer, 1).Value = Application.Transpose(FourthCellValues)
    End With
    End With[/VBA]

  4. #4
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Hi mikerickson,

    Thank you for the reply. It works perfectly, except for the deleting bit. The code bombs out in the line .Clear.Contents with the message of "Object Required" - Error message 424.

    Thanks

  5. #5
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Thanks CodeNinja,

    When I run the code it repeats the first row a number of times and deletes the rest.

  6. #6
    [vba]Sub snb()
    sn = Cells(1).CurrentRegion.Columns(1)
    ReDim sp(UBound(sn), 0)

    For j = 1 To UBound(sn) Step 4
    sp(j \ 4, 0) = sn(j, 1)
    Next

    Cells(1).CurrentRegion.Columns(1) = sp
    End Sub[/vba]

    or

    [VBA]Sub snb_oneliner()
    Cells(1).CurrentRegion.Columns(1) = Application.Index(Cells(1).CurrentRegion.Columns(1), [4*row(1:9)])
    End Sub[/VBA]
    Last edited by snb; 07-04-2012 at 01:04 AM.

  7. #7
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Whoa! This is impressive. The one liner doesn't work that well. It skips the first value and the bottom rows of the resultant list contains "N/A"s.

    In the first sub, how would you change it to work only on a selected portion of a column, in other words you would only like to manipulate the selected rows in a column.

    Thanks again for the excellent reply.

  8. #8
    It skips the first value and the bottom rows of the resultant list contains "N/A"s.
    Reply
    [vba]Sub snb_twoliner()
    With Cells(1).CurrentRegion.Columns(1)
    .Value = Application.Index(.Value, [4*(row(1:9)-1)+1])
    .SpecialCells(2, 16).ClearContents
    End With
    End Sub[/vba]

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    To fix my code, remove the . in Clear.Contents

  10. #10
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Hi mikerickson,

    It is working beautifully, thanks. How would you change it to work on a selected portion of a column only? In other words the first cell of the selection stays in place and the rest of the selection is dealt with as above, while the rest of the column and other columns stay intact.

    Thanks

  11. #11
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Hi snb,

    Thank you for all your effort - much appreciated. I love the conciseness of your code. Please could you explain what the following line means?
    sp(j \ 4, 0) = sn(j, 1) Thanks

  12. #12
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location
    Hi snb,

    One last question, I hope. How would you change it to work on a selected portion of a column only? In other words the first cell of the selection stays in place and the rest of the selection is dealt with as above, while the rest of the column and other columns stay intact.
    Sub snb() sn = Cells(1).CurrentRegion.Columns(1) ReDim sp(UBound(sn), 0) For j = 1 To UBound(sn) Step 4 sp(j \ 4, 0) = sn(j, 1) Next Cells(1).CurrentRegion.Columns(1) = sp End Sub

  13. #13
    You could use:
    [VBA]Sub snb_threeliner()
    With Selection
    Cells(1, 27) = .Cells(1).Row
    .Value = Application.Index(.Value, [4*(row(1:9)-1)+AA1])
    .SpecialCells(2, 16).ClearContents
    End With
    End Sub[/VBA]

Posting Permissions

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