Sort is not sorting all data in the same row....Really!

CastingDirector

New Member
Joined
Jun 10, 2014
Messages
46
I would be so grateful for any/all help.
So here is the data BEFORE the sort macro (note actor A has corresponding notes in the same row (e)):
[TABLE="width: 705"]
<tbody>[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD] Actor D[/TD]
[TD][/TD]
[TD]Carol|Notes on Actor D[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD] Actor E[/TD]
[TD][/TD]
[TD]Ted|Notes on Actor E[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD] Actor F[/TD]
[TD][/TD]
[TD]Ted|Notes on Actor F[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD] Actor G[/TD]
[TD][/TD]
[TD]Bob|No Notes[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD] Actor H[/TD]
[TD][/TD]
[TD]Ted|Notes on Actor H[/TD]
[/TR]
[TR]
[TD]NA[/TD]
[TD]10/02/15[/TD]
[TD] Actor C[/TD]
[TD][/TD]
[TD]Bob|Notes on Actor C[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD] Actor A[/TD]
[TD][/TD]
[TD]Carol|Notes on A[/TD]
[/TR]
[TR]
[TD]PASS[/TD]
[TD]10/02/15[/TD]
[TD] Actor B[/TD]
[TD][/TD]
[TD]Alice|Notes on B[/TD]
[/TR]
</tbody>[/TABLE]
NOW AFTER THE CODE is processed with the macro:
[TABLE="width: 705"]
<tbody>[TR]
[TD][/TD]
[TD]Alice[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD]Actor D[/TD]
[TD][/TD]
[TD]Notes on B[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Bob[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD]Actor E[/TD]
[TD][/TD]
[TD]No Notes[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD]Actor F[/TD]
[TD][/TD]
[TD]Notes on Actor C[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Carol[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD]Actor G[/TD]
[TD][/TD]
[TD]Notes on A[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD]Actor H[/TD]
[TD][/TD]
[TD]Notes on Actor D[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Ted[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]NA[/TD]
[TD]10/02/15[/TD]
[TD]Actor C[/TD]
[TD][/TD]
[TD]Notes on Actor E[/TD]
[/TR]
[TR]
[TD]NAA[/TD]
[TD]10/02/15[/TD]
[TD]Actor A[/TD]
[TD][/TD]
[TD]Notes on Actor F[/TD]
[/TR]
[TR]
[TD]PASS[/TD]
[TD]10/02/15[/TD]
[TD]Actor B[/TD]
[TD][/TD]
[TD]Notes on Actor H[/TD]
[/TR]
</tbody>[/TABLE]
I am thrilled it works...except....what the?!? The notes in column E do not correspond to the actor?!?

Here is the code...can you see where I've gone wrong? Could really use your help. I promise to give back when I can!
Code:
Option Explicit    Sub CastingDirector()
Dim sh As Worksheet, LR As Long, spl As Variant, i As Long, r As Long
Dim NR As Long
Dim Arange As Range
Dim x As Long
Dim BR As Long
    Set sh = Sheet4
NR = sh.Cells(Rows.Count, "E").End(xlUp).Row
    For r = NR To 6 Step -1
    If InStr(1, sh.Cells(r, 5).Value, "|") > 0 Then
         Set Arange = Range("E6:E" & NR)
         With Arange
         .Sort Key1:=Arange, Order1:=xlAscending, Header:=False
         End With
    End If
    Next
LR = sh.Cells(Rows.Count, "B").End(xlUp).Row
    For i = LR To 6 Step -1
       If sh.Cells(i, 5) Like "*|*" Then
        spl = Split(sh.Cells(i, 5).Value, "|")
        sh.Rows(i).Insert
        sh.Range("B" & i) = Trim(spl(LBound(spl)))
        With sh.Range("B" & i)
        .Font.Bold = True
        .Font.Size = 14
        End With
        sh.Range("E" & i + 1) = Trim(spl(UBound(spl)))
    End If
    Next
    BR = Range("A350").End(xlUp).Row
    For x = BR To 6 Step -1
    If Application.WorksheetFunction.CountIf(Range("B6:B" & x), Range("B" & x).Text) > 1 Then
    Range("A" & x).EntireRow.Delete
    End If
       
    Next x
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi,

I can't work out what your code is supposed to do but the error is in here:
Code:
         Set Arange = Range("E6:E" & NR)
         With Arange
         .Sort Key1:=Arange, Order1:=xlAscending, Header:=False
         End With
You need to tell it both the range to sort and the key. Currently, you are telling if to sort the range Arange only.
Unfortunately, there are lots of ways to do this but I will have to pick only one - and I don't know what range you are trying to sort.

Code:
    Range("A6:E" & NR).Sort Key1:=Range("E6"), Order1:=xlAscending, Header:=False
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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