billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Afternoon

Is there a way to filter (smallest to largest) East Region in column D "Sales" without the rows below altered. The Entire row of the East region will be moved accordingly.

IDRegionCodeSales
101Eastw300
102Wests200
103Northd100
104Southw200
105Easts250
106Westd200
107Northw150
108Souths230
109Eastd245
110Westd225
111Northw125
112Southd180

<tbody>
</tbody>



After


IDRegionCodeSales
109Eastd245
102Wests200
103Northd100
104Southw200
105Easts250
106Westd200
107Northw150
108Souths230
101Eastw300
110Westd225
111Northw125
112Southd180

<tbody>
</tbody>
 
Last edited:

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.
I can't think of any built-in tools that can do that. It would probably be possible to create some formulas to do that, but the result would be a different table with the original table remaining intact.

It sounds like you need a VBA macro to sort the East entries, while leaving the other entries alone. A somewhat easier proposition. But how would you want to define your desires? I notice that the South entries did not move, even though the last row could have moved up. Is it always the East region? Or do you want to select which region?
 
Upvote 0
Open a copy of your workbook. Press Alt-F11 to open the VBA editor. From the menu, select Insert > Module. In the window that opens, paste this code:

Rich (BB code):
Sub SortEast()
Dim i As Long, j As Long, r As Long, lr As Long, e As Variant
Dim SortArr As Object, arr1 As Variant, arr2 As Variant, EastRows() As Long

' Macro to sort just elements in a table labeled "East", all others remain in place
' https://www.mrexcel.com/forum/excel-questions/1082051-filter.html
' Eric W. 12/29/2018

    lr = Cells(Rows.Count, "A").End(xlUp).Row
    arr1 = Range("A2:D" & lr).Value
    arr2 = Range("A2:D" & lr).Value
    Set SortArr = CreateObject("System.Collections.ArrayList")
    ReDim EastRows(1 To lr)
    r = 0
    
    For i = 1 To UBound(arr1)
        If arr1(i, 2) = "East" Then
            SortArr.Add Format(arr1(i, 4), "0000000000.00000") & Format(i, "0000000")
            r = r + 1
            EastRows(r) = i
        End If
    Next i
    SortArr.Sort
    i = 0
    For Each e In SortArr
        r = Right(e, 6)
        i = i + 1
        For j = 1 To 4
            arr1(EastRows(i), j) = arr2(r, j)
        Next j
    Next e
    
    Range("A2:D" & lr).Value = arr1
End Sub
Change the values in red to match your requirements. Go back to your Excel sheet with the table in it. Press Alt-F8 to open the macro selector, choose SortEast and click Run.
 
Upvote 0
Here is another macro that you can consider (roughly the same underlying principle as Eric's macro, but more compact)...
Code:
[table="width: 500"]
[tr]
	[td]Sub IndividualRegionSort()
  Dim R As Long, Arr As Variant, Parts As Variant, Rws As New Collection
  With CreateObject("System.Collections.ArrayList")
    For R = 2 To Cells(Rows.Count, "B").End(xlUp).Row
      If Cells(R, "B").Value = "[B][COLOR="#FF0000"]East[/COLOR][/B]" Then
        .Add Format(Cells(R, "D").Value, "0000000000.00000|") & Cells(R, "A").Value & "|" & Cells(R, "C").Value
        Rws.Add R
      End If
    Next
    .Sort
    Arr = .ToArray
    For R = 0 To .Count - 1
      Parts = Split(Arr(R), "|")
      Cells(Rws(R + 1), "A").Resize(, 4) = Array(Parts(1), "[B][COLOR="#FF0000"]East[/COLOR][/B]", Parts(2), Val(Parts(0)))
    Next
  End With
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
That is equally incredible.

Thank You Mr. Rothstein.

The values in "Red" East are interchangeable?

If there are addtional columns i assume this can be amended accordingly as well using the delimiter "|" & cells property.
 
Upvote 0
The values in "Red" East are interchangeable?
Yes, if you want to sort West, just change the two red "East" to "West" (you can do that on Eric's code as well).



If there are addtional columns i assume this can be amended accordingly as well using the delimiter "|" & cells property.
Yes, but you would need to change the Resize from 4 columns to whatever the new column count is and, of course, add the appropriate Parts array element to the Array function call. If you want to add more columns and the above is too cryptic for you, just post back with complete details and I (or someone else) will be happy to modify the code for you.
 
Upvote 0
Thanks again Mr. Rothstein

I'm trying to learn how to fish, hoping to ask for assistance when absolutely necessary.
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,717
Members
449,116
Latest member
Aaagu

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