Sorting a table range using vba

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
I have the following.
Row 1 is headers.
Row 2 is hidden.
My range is then A3:G3 then down the page.

I wish to sort this range from A-Z using column A
Its within a Table of which is Table 28

I have tried the following BUT i get a RTE1004 Application defined or Object defined error.
The code in blue below is shown when i debug it
Do you see where the issue is

Rich (BB code):
Private Sub CommandButton1_Click()
    Workbooks.Open ("C:\Users\Ian\Desktop\REMOTES ETC\DR\EXCEL WORKSHEETS\MOTORCYCLES.xlsm")
    Worksheets("INVOICES").Activate
    Worksheets("INVOICES").Rows("3:3").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveWorkbook.Close SaveChanges:=True
   
    Set WB = Workbooks.Open(fileName:="C:\Users\Ian\Desktop\REMOTES ETC\DR\EXCEL WORKSHEETS\MOTORCYCLES.xlsm")
        Workbooks("DR.xlsm").Sheets("INV").Range("G13").Copy
        WB.Sheets("INVOICES").Range("A3").PasteSpecial xlPasteValues
       
        Workbooks("DR.xlsm").Sheets("INV").Range("L16").Copy
        WB.Sheets("INVOICES").Range("B3").PasteSpecial xlPasteValues
       
        Workbooks("DR.xlsm").Sheets("INV").Range("L15").Copy
        WB.Sheets("INVOICES").Range("C3").PasteSpecial xlPasteValues
       
        Workbooks("DR.xlsm").Sheets("INV").Range("H52").Copy
        WB.Sheets("INVOICES").Range("D3").PasteSpecial xlPasteValues
       
        Workbooks("DR.xlsm").Sheets("INV").Range("H53").Copy
        WB.Sheets("INVOICES").Range("E3").PasteSpecial xlPasteValues
       
        Workbooks("DR.xlsm").Sheets("INV").Range("L13").Copy
        WB.Sheets("INVOICES").Range("F3").PasteSpecial xlPasteValues
       
        Workbooks("DR.xlsm").Sheets("INV").Range("L4").Copy
        WB.Sheets("INVOICES").Range("G3").PasteSpecial xlPasteValues
       
    Dim X As Long
        Application.ScreenUpdating = False
    With Sheets("INVOICES")
        If .AutoFilterMode Then .AutoFilterMode = False
        X = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Range("A2:G" & X).Sort Key1:=.Cells(A3, ColumnNum), Order1:=xlAscending, Header:=xlGuess, dataoption1:=xlSortTextAsNumbers
    End With
       
        WB.Close True
       
        Workbooks("DR.xlsm").Sheets("INV").Range("G2").Select
        Application.CutCopyMode = False
        ActiveWorkbook.Save


End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
In key1 you must reference a cell.
If you are in a table, you must indicate from row 1, Heade:=XlYes.
The row you have hidden will not be sorted.

Try this:
Rich (BB code):
  Dim x As Long
  Application.ScreenUpdating = False
 
  With Sheets("INVOICES")
    If .AutoFilterMode Then .AutoFilterMode = False
    x = .Cells(.Rows.Count, 1).End(xlUp).Row
    .Range("A1:G" & x).Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlYes, DataOption1:=xlSortTextAsNumbers
  End With

:cool:
 
Upvote 0
Solution

Forum statistics

Threads
1,215,095
Messages
6,123,073
Members
449,093
Latest member
ripvw

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