VBA: Move Some Rows

Rixn

Board Regular
Joined
Jun 4, 2005
Messages
117
Office Version
  1. 2016
Platform
  1. Windows
I need help in moving some rows.

I've got this in sheet "Import":
Excel Workbook
DEF
10Alpha1Orange
11Alpha2Light Green
12Alpha3Red
13Alpha4Dark Blue
14Charlie1Blue
15Charlie2Dark Brown
16Zulu1Purple
17Zulu2Black
18Zulu3Gray
19Delta1Dark Purple
20Delta2Green
21Delta3Light Blue
22Zulu1Brown
23Zulu2Yellow
24Zulu3Dark Red
25Beta1Pink
26Beta2White
27Beta3Dark Green
28Beta4Beige
Sheet
..and I've extracted the information of which rows it is where every new unique entry occurs (I keep it in sheet "SE"):
Excel Workbook
AB
1Alpha10
2Charlie14
3Zulu16
4Delta19
5Zulu22
6Beta25
7*29
Sheet
My objective is to rearrange the rows of the initial table so the "Zulu" rows ends up last, like this:
Excel Workbook
DEF
10Alpha1Orange
11Alpha2Lighr Green
12Alpha3Red
13Alpha4Dark Blue
14Charlie1Blue
15Charlie2Dark Brown
16Delta1Dark Purple
17Delta2Green
18Delta3Light Blue
19Beta1Pink
20Beta2White
21Beta3Dark Green
22Beta4Beige
23Zulu1Purple
24Zulu2Black
25Zulu3Gray
26Zulu1Brown
27Zulu2Yellow
28Zulu3Dark Red
Sheet
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
No, I want to move the Zulu rows to the end of the list.
 
Upvote 0
Maybe this...

Puts the arranged data on a new sheet (named New)

Code:
Sub arrData()
    Dim arrOthers() As Variant, arrZulu() As Variant, numRows As Long
    Dim lastRow As Long, rngData As Variant, countZulu As Long, i As Long, j As Long, lin As Long
    
    With Sheets("Import")
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
        numRows = .Range("D10:F" & lastRow).Rows.Count
        countZulu = Application.CountIf(.Range("D10:D" & lastRow), "Zulu")
                
        rngData = .Range("D10:F" & lastRow).Value
                
        ReDim arrOthers(1 To numRows - countZulu, 1 To 3)
        ReDim arrZulu(1 To countZulu, 1 To 3)
        
        For lin = 1 To numRows
            If rngData(lin, 1) = "Zulu" Then
                i = i + 1
                arrZulu(i, 1) = rngData(lin, 1)
                arrZulu(i, 2) = rngData(lin, 2)
                arrZulu(i, 3) = rngData(lin, 3)
            Else
                j = j + 1
                arrOthers(j, 1) = rngData(lin, 1)
                arrOthers(j, 2) = rngData(lin, 2)
                arrOthers(j, 3) = rngData(lin, 3)
            End If
        Next lin
    End With
    
    Sheets.Add
    ActiveSheet.Name = "New"
    
    With Sheets("New")
       .Range("D10").Resize(numRows - countZulu, 3) = arrOthers
       .Range("D10").Offset(numRows - countZulu).Resize(countZulu, 3) = arrZulu
    End With
        
End Sub

M.
 
Upvote 0
Code:
Sub a()
ThisWorkbook.Sheets.Add.Name = "Service"
Application.DisplayAlerts = False
With Sheets("Import")
  LR = .Cells(.Rows.Count, "D").End(xlUp).Row
  drow = 1
  For j = 10 To LR
    If .Range("D" & j) = "Zulu" Then
      .Range("D" & j & ":F" & j).Copy Sheets("Service").Cells(drow, 1)
      drow = drow + 1
      .Rows(j).Delete
      j = j - 1
    End If
  Next
  LR = .Cells(.Rows.Count, "D").End(xlUp).Row
  Sheets("Service").Range("A1:C" & drow).Copy .Range("D" & LR + 1)
  Sheets("Service").Delete
End With
End Sub
 
Upvote 0
@Marcelo Branco
Thanx for your suggestion.
I looked into it, but I'm looking for a solution that just re-arrange the existing list in D10:F28 in sheet "Import". I lack knowledge to adjust your suggestion into what I want.
 
Upvote 0
@patel45
Thanx for your suggestion too!
I think I get how it works, but I'm not sure about the "Service" part - can you please explain it a bit more, maybe comment the rows of importance so I know what's happening?
 
Upvote 0
@Marcelo Branco
Thanx for your suggestion.
I looked into it, but I'm looking for a solution that just re-arrange the existing list in D10:F28 in sheet "Import". I lack knowledge to adjust your suggestion into what I want.

Maybe...

Try it in a copy of your workbook

Code:
Sub arrTest()
    Dim arrOthers() As Variant, arrZulu() As Variant, rcell As Range
    Dim lastRow As Long, rngData As Range, countZulu As Long, i As Long, j As Long
    
    With Sheets("Import")
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
        Set rngData = .Range("D10:F" & lastRow)
        countZulu = Application.CountIf(rngData.Columns(1), "Zulu")
        
        ReDim arrOthers(1 To rngData.Rows.Count - countZulu, 1 To 3)
        ReDim arrZulu(1 To countZulu, 1 To 3)
        
        For Each rcell In rngData.Columns(1).Cells
            If rcell = "Zulu" Then
                i = i + 1
                arrZulu(i, 1) = rcell.Value
                arrZulu(i, 2) = rcell.Offset(, 1).Value
                arrZulu(i, 3) = rcell.Offset(, 2).Value
            Else
                j = j + 1
                arrOthers(j, 1) = rcell.Value
                arrOthers(j, 2) = rcell.Offset(, 1).Value
                arrOthers(j, 3) = rcell.Offset(, 2).Value
            End If
        Next rcell
        
        .Range("D10").Resize(rngData.Rows.Count - countZulu, 3) = arrOthers
        .Range("D10").Offset(rngData.Rows.Count - countZulu).Resize(countZulu, 3) = arrZulu
    End With
        
End Sub

M.
 
Upvote 0
@patel45
Thanx for your suggestion too!
I think I get how it works, but I'm not sure about the "Service" part - can you please explain it a bit more, maybe comment the rows of importance so I know what's happening?
1) create service sheet
2) copy ranges with zulu to service sheet
3) delete rows with zulu on sheet Import
4) copy ranges with zulu to import sheet
5) delete service sheet
 
Upvote 0
@Marcelo Branco
Thanx again!
As you can see @patel45 also submitted a solution. May I ask what is better with your solution?
(and if you have time, can you please comment the important rows so I can learn better what your code do?)
 
Upvote 0

Forum statistics

Threads
1,203,640
Messages
6,056,491
Members
444,869
Latest member
tulo spont

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top