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
 
Akuini,

Seems to work after slight adjustment to the code see below. Once but soon as i try the next number on the list it stops working.

VBA Code:
Sub Number_Sort()

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

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
    
    Set ws = ActiveSheet
    Lr = ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
    Set Rng = ws.Range("A1:A" & Lr)
    
    For Each v In Rng
        If IsNumeric(v) Then
            v.Value = "'" & v.Formula
        End If
    Next
    
    With ws
        Range("A2:H" & Lr).Sort , _
                      Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    End With
    
End Sub
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Let's try referencing the sheet properly.
VBA Code:
Sub try2()
Dim c As Range
Dim Ws As Worksheet
Dim LR As Long
Set Ws = ThisWorkbook.Worksheets("Frost Drains")
    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("A1:H" & LR)
        .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    End With
End Sub
 
Upvote 0
I tried to convert values back to numbers for next time you run the code which works.
Just not sure why the code above won`t recognize number format the next time you need to add a number??

VBA Code:
Public Sub Text_to_Number()

    Dim Ws     As Worksheet
    Dim Rng    As Range
    Dim v      As Range
    Dim LR     As Long
    Dim N As Variant

    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:A" & LR)
    
    With Ws
    For Each N In Rng
        If IsNumeric(N) Then
            N.Value = CSng(N.Value)
            N.NumberFormat = "0.0000"
        End If
    Next
    End With
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
        .Calculation = xlCalculationAutomatic
    End With
    
End Sub
 
Upvote 0
It`s now working see code alteration below.

VBA Code:
Sub Number_Sort()

    Dim c      As Range
    Dim Ws     As Worksheet
    Dim LR     As Long
    Dim Rng    As Range

    Set Ws = ThisWorkbook.Worksheets("Frost Drains")
    LR = Ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
    Set Rng = Ws.Range("A1:A" & LR)
    
    For Each c In Rng
        If Application.WorksheetFunction.IsNumber(c.Value) = True Then
            c.Value = "'" & c.Formula
        End If
    Next
    
    With Ws.Range("A1:H" & LR + 1)
        .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    End With
    
    Call Text_to_Number
    
End Sub
 
Upvote 0
Solution
1. I forgot, the code in post #22 (and also all before that) this part:
VBA Code:
LR = Ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
should be
VBA Code:
LR = Ws.Cells(Rows.Count, 1).End(xlUp).Row


2. This part of your code:
If Application.WorksheetFunction.IsNumber(c.Value) = True Then

Interesting, I don't know why changing the earlier code with this makes any differences. But glad it works now.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

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