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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Will this change a range of cells rather than just A column?
Ah, sorry the sort part should include col A:H:
VBA Code:
Sub try2()
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:H")
        .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    End With
End Sub
 
Upvote 0
Do you mean the numbers didn't become text?
 
Upvote 0
I don't understand your answer, my question was:
Did the numbers in col A change to text?
 
Upvote 0
Column A values changed position like they should (That`s perfect) but the rest of the range did not change. Do we need the entire row command in the code?
 
Upvote 0
I did sorry can`t work it out.
Not sure why.
Could you upload a sample workbook (without sensitive data) to a sharing site like dropbox.com or google drive?
And then share the link here.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,953
Members
449,095
Latest member
nmaske

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