Needing code help to sort data

Bret1

Board Regular
Joined
Jun 14, 2013
Messages
201
I'm in need of help to automate sorting data. I need the code to compare #s down columns "K" and "P", starting at the row the cursor currently is and ending at 1st blank cell in "K". I've recorded a Macro and added comments on what I'm trying to do and added below... Thanks for any help you can provide to automate this task I've been doing manually!
VBA Code:
Sub Shift_Rows()
'
' Shift_Rows Macro
'
        'Compare columns "K" & "P" searching for unequal numbers starting at current curser location row
        'Row("12:12") is 1st row where column "K" and "P" don't equal
        'Insert Row at row 12:12
    Rows("12:12").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        'Copy/Paste "M" through "S" on previous row down to "M12"
    Range("M11:S11").Select
    Selection.Copy
    Range("M12").Select
    ActiveSheet.Paste
        'Select data down "K" starting at next row (K13) down to 1st blank cell and shift those cell up one cell
        'The 1st blank cell here is at (K45)
    Range("K13:K45").Select
    ActiveWindow.SmallScroll Down:=-12
    Application.CutCopyMode = False
    Selection.Cut Destination:=Range("K12:K44")
        'Copy new number in "K12" and paste in "P12"
        'Continue to next row repeating process until the 1st blank cell in "K"
    Range("K12").Select
    Selection.Copy
    Range("P12").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
See if this works for you :

VBA Code:
Option Explicit

 Sub SortColumnsKP_Numerically_NoHelper()
    Dim ws As Worksheet
    Dim startRow As Long, endRow As Long
    Dim dataRange As Range

    ' Set the worksheet and current position
    Set ws = ActiveSheet
    
    ' Determine the start row (current cursor position)
    startRow = ActiveCell.Row

    ' Determine the end row (first blank cell in column K)
    endRow = startRow
    Do While Not IsEmpty(ws.Cells(endRow, "K"))
        endRow = endRow + 1
    Loop
    endRow = endRow - 1 ' Adjust to the last non-empty row in column K

    ' Ensure values in columns K and P are converted to numeric
    Dim cell As Range
    For Each cell In ws.Range("K" & startRow & ":K" & endRow)
        If IsNumeric(Mid(cell.Value, 2)) Then
            cell.Value = Val(Mid(cell.Value, 2))
        End If
    Next cell
    
    ' Define the range to sort (columns K to P in the specified range)
    Set dataRange = ws.Range("K" & startRow & ":P" & endRow)

    ' Perform sorting by column K (numeric), then column P
    dataRange.Sort Key1:=ws.Range("K" & startRow), Order1:=xlAscending, _
                   Key2:=ws.Range("P" & startRow), Order2:=xlAscending, _
                   Header:=xlNo
                  
    ' Restore column K values to original format with "K" prefix
    For Each cell In ws.Range("K" & startRow & ":K" & endRow)
        cell.Value = "K" & cell.Value
    Next cell

    MsgBox "Sorting completed from row " & startRow & " to row " & endRow & ".", vbInformation
End Sub
 
Upvote 0
See if this works for you :

VBA Code:
Option Explicit

 Sub SortColumnsKP_Numerically_NoHelper()
    Dim ws As Worksheet
    Dim startRow As Long, endRow As Long
    Dim dataRange As Range

    ' Set the worksheet and current position
    Set ws = ActiveSheet
   
    ' Determine the start row (current cursor position)
    startRow = ActiveCell.Row

    ' Determine the end row (first blank cell in column K)
    endRow = startRow
    Do While Not IsEmpty(ws.Cells(endRow, "K"))
        endRow = endRow + 1
    Loop
    endRow = endRow - 1 ' Adjust to the last non-empty row in column K

    ' Ensure values in columns K and P are converted to numeric
    Dim cell As Range
    For Each cell In ws.Range("K" & startRow & ":K" & endRow)
        If IsNumeric(Mid(cell.Value, 2)) Then
            cell.Value = Val(Mid(cell.Value, 2))
        End If
    Next cell
   
    ' Define the range to sort (columns K to P in the specified range)
    Set dataRange = ws.Range("K" & startRow & ":P" & endRow)

    ' Perform sorting by column K (numeric), then column P
    dataRange.Sort Key1:=ws.Range("K" & startRow), Order1:=xlAscending, _
                   Key2:=ws.Range("P" & startRow), Order2:=xlAscending, _
                   Header:=xlNo
                 
    ' Restore column K values to original format with "K" prefix
    For Each cell In ws.Range("K" & startRow & ":K" & endRow)
        cell.Value = "K" & cell.Value
    Next cell

    MsgBox "Sorting completed from row " & startRow & " to row " & endRow & ".", vbInformation
End Sub
I couldn't get that to run. Here is an image that may help show what I'm needing to do...
 

Attachments

  • Row Sorter GIF.gif
    Row Sorter GIF.gif
    120.1 KB · Views: 10
Upvote 0

Forum statistics

Threads
1,224,944
Messages
6,181,927
Members
453,072
Latest member
jtees4

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