Need excel to sort first 7 values only)

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Trying to use a loop to sort first 7 values only in A2 to last row with Range A2:H to last row?

VBA Code:
Sub Number_Sort()

    Dim ws     As Worksheet
    Dim Lr     As Long
    Dim Rng    As Range
    Dim v      As Range

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
    
    
    
    Set ws = ThisWorkbook.Worksheets("Frost Drains")
    Lr = ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
    Set Rng = ws.Range("A2:H" & Lr)
    
    With ws
        For Each v In .Range("A2:A" & Lr)
            v.Offset(0, 1).Value = Val(Mid(v, Evaluate("=MIN(FIND({0,1,2,3,4,5,6,7}," & v.Address & "&""01234567""))")))
            .Range("A:H").Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlYes
            Rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
        Next v
    End With
i
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
        .Calculation = xlCalculationAutomatic
    End With
    
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Could you post an example (preferably by using XL2BB)? Create a mock up data, say 10 rows, and also show us what the result should look like.
 
Upvote 0
Part NoDateIssueDrawn ByDesign ByDescriptionSizeMaterial
33.041428/06/20231DLSFrost182L 100dCast Iron & Aluminium
33.043327/06/20231DLSFrost182L 100dCast Iron & Aluminium
33.043419/06/20231DLSFrostFrost Grating adjustable 182mm Square threaded BSP4182SQ 100dAluminium
31.0220.P18/05/20231DLSFrostFrost Floor drain 150mm square nickel bronze grating with large sump body and spigot outlet150SQ 190dCast Iron + Nickel Bronze
31.0221.P18/05/20231DLSFrostFrost floor drain 150mm square nickel bronze grating with medium sump - 'P' trap - spigot outlet198Ø 110dCast Iron + Stainless Steel
31.0221.SC18/05/20231DLSFrostFrost floor drain 150mm square nickel bronze grating with medium sump - 'S' trap - spigot outlet198Ø 190dCast Iron + Stainless Steel
33.0411.928/06/20231DLSFrostCast Iron & Aluminium
 
Upvote 0
As you can see above the 33.0411.9 Needs to be above 33.0414. Sorry can`t get XL2BB to work at the moment.
 
Upvote 0
I don't understand, when I sort the data manually here's the result:
Book2
ABCDEFGH
1Part NoDateIssueDrawn ByDesign ByDescriptionSizeMaterial
231.0220.P18/05/20231DLSFrostFrost Floor drain 150mm square nickel bronze grating with large sump body and spigot outlet150SQ 190dCast Iron + Nickel Bronze
331.0221.P18/05/20231DLSFrostFrost floor drain 150mm square nickel bronze grating with medium sump - 'P' trap - spigot outlet198Ø 110dCast Iron + Stainless Steel
431.0221.SC18/05/20231DLSFrostFrost floor drain 150mm square nickel bronze grating with medium sump - 'S' trap - spigot outlet198Ø 190dCast Iron + Stainless Steel
533.0411.928/06/20231DLSFrostCast Iron & Aluminium
633.041428/06/20231DLSFrost182L 100dCast Iron & Aluminium
733.043327/06/20231DLSFrost182L 100dCast Iron & Aluminium
833.043419/06/20231DLSFrostFrost Grating adjustable 182mm Square threaded BSP4182SQ 100dAluminium
Sheet5


which part of the result does not meet your criteria?
 
Upvote 0
When sort manually it works but i need to use VBA for this.
So that`s where i have got first seven values in my code then how can i use just those values to sort data?
 
Upvote 0
Sample in post #3, the values in A2:A4, are they number or text?
 
Upvote 0
Numbers
Any cells with letters i need to ignore any letters just use the numbers to sort.
 
Last edited:
Upvote 0
Numbers
Any cells with letters i need to ignored any letters just use the numbers to sort.
Having text & number data type in the same column will make it harder to deal wtih.
This code will change the number to text by adding apostrophe in front of it and then sort the data.
VBA Code:
Sub try()
Dim c As Range
Set ws = ActiveSheet
    Lr = ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
    Set Rng = ws.Range("A1:A" & Lr)

    For Each c In Rng
        If IsNumeric(c) Then
            c.Value = "'" & c.Formula
        End If
    Next
  
    With ws.Range("A:A")
        .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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