Sort Rows by Multiple Columns

drew27c

New Member
Joined
Aug 16, 2016
Messages
24
I have a macro that loops through all the XLSX files in a given folder and formats them one by one. I need the very last formatting step to sort by Column B (STREET NAME) and then by Column C (HOUSE NUMBER). I have tried a few things but they always sort just the two columns, and not reordering all the rows. Often times row E has no data in it, not sure if that will break sorting logic.

The XLSX files will always have a different amount of rows, but I don't thnik that should affect sorting if selecting the whole column. Perhaps I am wrong.

Any help would be appreciated.


tAiGL84.png





Code:
Sub ProcessFiles()
    Dim Filename, Pathname As String
    Dim wb As Workbook
 
    Pathname = ActiveWorkbook.Path & "C:\Users\me\Desktop\Loop\"
    Filename = Dir(Pathname & "*.xlsx")
    Do While Filename <> ""
        Set wb = Workbooks.Open(Pathname & Filename)
        DoWork wb
        wb.Close SaveChanges:=True
        Filename = Dir()
    Loop
End Sub
 
Sub DoWork(wb As Workbook)
    With wb
        'Do your work here
 
'DELETE LABEL ROW
'    Rows("2:2").Select
'    Selection.Delete Shift:=xlUp
'    Range("A1").Select
 
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:N2"), Type:=xlFillCopy
    Range("B2:N2").Select
    Range("A1").Select
 
'RESIZE FONT AND ROW HEIGHT, JUSTIFY, AUTOFIT
    Cells.Select
    With Selection.Font
        .Size = 8
    End With
    Selection.RowHeight = 45
    Columns("A:O").HorizontalAlignment = xlCenter
    Columns("A:O").EntireColumn.AutoFit
       
'HEADER LABELS
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "HOUSE" & Chr(10) & "NUMBER"
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "STREET" & Chr(10) & "NAME"
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "STREET" & Chr(10) & "TYPE"
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "FIRST" & Chr(10) & "NAME"
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "LAST" & Chr(10) & "NAME"
    Range("L1").Select
    ActiveCell.FormulaR1C1 = "INTERNET" & Chr(10) & "PRODUCT"
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "FIBETV" & Chr(10) & "PRODUCT"
    Range("N1").Select
    ActiveCell.FormulaR1C1 = "HOMEPHONE" & Chr(10) & "PRODUCT"
 
'REMOVE UNECCESSARY COLUMNS, FORMAT HEADERS
    Range("A:A,B:B,C:C,E:E").Select
    Selection.Delete Shift:=xlToLeft
    Columns("A:A").Select
    Range("A1:J1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    Selection.Font.Bold = True
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With
    Columns("A:O").EntireColumn.AutoFit
    Range("A1").Select
  
'ADD PRINT GRID AND FORMATS
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = ""
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.708661417322835)
        .RightMargin = Application.InchesToPoints(0.708661417322835)
        .TopMargin = Application.InchesToPoints(0.748031496062992)
        .BottomMargin = Application.InchesToPoints(0.748031496062992)
        .HeaderMargin = Application.InchesToPoints(0.31496062992126)
        .FooterMargin = Application.InchesToPoints(0.31496062992126)
        .PrintHeadings = False
        .PrintGridlines = True
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = 100
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    Application.PrintCommunication = True
 
'DELETE FSA, SAVE AND CLOSE
    'Columns(11).EntireColumn.Delete
    Range("A1").Select
    ActiveWorkbook.Save
    'ActiveWorkbook.Close True
    End With
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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