VBA in Microsoft Excel Version 365

lcoleman1412

New Member
Joined
Mar 25, 2025
Messages
1
Office Version
  1. 365
Platform
  1. Windows
My boss would like me to insert a button in cell C25. He wants to be able to click the button and be able to sort the data. When I tried to create and insert a button I plugged in a VBA code and it sorted it but it would move all of my data up. I need to be able to sort it while leaving rows 1, 2, 6,16 ,22, 23 with the headings, #'s 1,2,3,4, and CDT-Recall-8 in the same place. Could anyone please help me with a code that can sort the data on the sheet and be able to sort any new data inserted without moving those certain cells? My boss said he wants column B and C to sort and column C to be the key.



The first two images I have uploaded are images of my excel sheet. The last image is how my excel sheet turned out after I tried the VBA code.
Screenshot 2025-03-21 101124.png
Screenshot 2025-03-25 114615.png
Screenshot 2025-03-25 120152.png
 
Give it a go,
VBA Code:
Sub x1()
    Dim a As Worksheet
    Dim b As Long, c As Long, d As Long, e As Long

    Set a = ActiveSheet
    b = a.Cells(a.Rows.Count, "B").End(xlUp).Row
    c = 2

    Do While c <= b
        If IsNumeric(a.Cells(c, 1).Value) And a.Cells(c, 1).Value <> "" Then
            d = c + 1
            e = d

            Do While e <= b And a.Cells(e, 1).Value = ""
                e = e + 1
            Loop
            e = e - 1

            If e > d Then
                a.Sort.SortFields.Clear
                a.Sort.SortFields.Add Key:=a.Range("C" & d & ":C" & e), _
                    SortOn:=xlSortOnValues, Order:=xlAscending, _
                    DataOption:=xlSortNormal

                With a.Sort
                    .SetRange a.Range("B" & d & ":C" & e)
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
            End If
            c = e + 1
        Else
            c = c + 1
        End If
    Loop
    
End Sub
 
Upvote 0
VBA Code:
Sub Mysort()
Dim Arr As String, Arr1 As String
Dim Rng As Range
    For Each Rng In Worksheets(4).Range("A2:A23")
        If Rng.Value <> "" Then
            With Rng
                Arr = .Offset(1, 1).Address
                Arr1 = .Offset(1, 1).Offset(0, 1).Address
                    If .Offset(1, 1).End(xlDown).Row < 5000 Then
                        Arr = Arr & ":" & .Offset(1, 1).End(xlDown).Offset(0, 1).Address
                        Arr1 = Arr1 & ":" & .Offset(1, 1).End(xlDown).Offset(0, 1).Address
                    Else
                        Arr = Arr & ":" & .Cells(.Rows.Count, 3).Offset(0, 1).End(xlDown).Address
                        Arr1 = Arr1 & ":" & .Cells(.Rows.Count, 3).Offset(0, 1).End(xlDown).Address
                    End If
            End With
            With Worksheets("G")
                With .Sort
                        .SortFields.Add2 Key:=Range(Arr1), _
                         SortOn:=xlSortOnValues, Order:=xlAscending, _
                         DataOption:=xlSortNormal
                        .SetRange Range(Arr)
                        .Header = xlGuess
                        .MatchCase = False
                        .Orientation = xlTopToBottom
                        .SortMethod = xlPinYin
                        .Apply
                End With
            End With
        End If
    Next
End Sub
 
Upvote 0
VBA Code:
Sub Mysort()
Dim Arr As String, Arr1 As String
Dim Rng As Range
    For Each Rng In Worksheets("Sheet 3 (Sort)").Range("A2:A23")
        If Rng.Value <> "" Then
            With Rng
                Arr = .Offset(1, 1).Address
                Arr1 = .Offset(1, 1).Offset(0, 1).Address
                    If .Offset(1, 1).End(xlDown).Row < 5000 Then
                        Arr = Arr & ":" & .Offset(1, 1).End(xlDown).Offset(0, 1).Address
                        Arr1 = Arr1 & ":" & .Offset(1, 1).End(xlDown).Offset(0, 1).Address
                    Else
                        Arr = Arr & ":" & .Cells(.Rows.Count, 3).Offset(0, 1).End(xlDown).Address
                        Arr1 = Arr1 & ":" & .Cells(.Rows.Count, 3).Offset(0, 1).End(xlDown).Address
                    End If
            End With
            With Worksheets("Sheet 3 (Sort)")
                With .Sort
                        .SortFields.Add2 Key:=Range(Arr1), _
                         SortOn:=xlSortOnValues, Order:=xlAscending, _
                         DataOption:=xlSortNormal
                        .SetRange Range(Arr)
                        .Header = xlGuess
                        .MatchCase = False
                        .Orientation = xlTopToBottom
                        .SortMethod = xlPinYin
                        .Apply
                End With
            End With
        End If
    Next
End Sub
Rename worksheet to Worksheets("Sheet 3 (Sort)")
 
Upvote 0
lcoleman1412
You have more digits in col.C after sort.
Does it need to be considered?
Code:
Sub test()
    Dim x&, r As Range
    With Sheets("sheet 3 (sort)")
        x = .Evaluate(Replace("max(if(isnumber(#),row(#)))", "#", .UsedRange.Columns(3).Address))
        On Error Resume Next
        For Each r In .Range("b1:b" & x).SpecialCells(4)
            r(2, 0) = r(1, 0)
        Next
        .Range("b1:b" & x).SpecialCells(4).EntireRow.Delete
        With .Range("b2", .Range("b" & Rows.Count).End(xlUp)).Resize(, 2)
            .Sort .Columns(2)
        End With
    End With
End Sub
 
Upvote 0
Option
Sort sort.xlsm
ABCDEF
1Stage/Phase
21 
3CDT-Procedure11.1CDT-Procedure11.1
4CDT-Procedure22.1CDT-Procedure22.1
5CDT-Procedure33.1CDT-Procedure33.1
62
7CDT-Procedure12.2CDT-Procedure21.1
8CDT-Procedure21.1CDT-Procedure32.1
9CDT-Procedure32.1CDT-Procedure12.2
10CDT-Procedure44.3CDT-Procedure72.3
11CDT-Procedure54.1CDT-Procedure93.1
12CDT-Procedure64.2CDT-Procedure83.2
13CDT-Procedure72.3CDT-Procedure54.1
14CDT-Procedure83.2CDT-Procedure64.2
15CDT-Procedure93.1CDT-Procedure44.3
163
17CDT-Procedure12.3CDT-Procedure51.1
18CDT-Procedure22.1CDT-Procedure31.2
19CDT-Procedure31.2CDT-Procedure22.1
20CDT-Procedure42.2CDT-Procedure42.2
21CDT-Procedure51.1CDT-Procedure12.3
224
23CDT-Recall4.1CDT-Recall4.1
245
S
Cell Formulas
RangeFormula
E2:F23E2=SORTBY(B2:C23,C2:C23+LET(X,A2:A24,Y,TEXTJOIN("",0,IFERROR(REPT(","&ROW(1:5),UNIQUE(MATCH(A3:A24,A3:A24),0,1)+1-UNIQUE(MATCH(X,X),0,1)),"")),MID(Y,FIND("|",SUBSTITUTE(Y,",","|",ROW(1:22)))+1,1)*100))
Dynamic array formulas.
 
Upvote 0

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