VBA - Sort Table in Alternate Workbook

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I am trying to get my vba to sort a table for me in a different workbook. I've got about 400 of these project files to do, all being created from the same master file, meaning in all instances, the worksheet and table names are the same.

Where I get hung up is where I add my sort key (I've got it commented at the end of that problem line). I'm getting a Run-time error '1004': Method 'Range' of object'_Worksheet' failed.

I copied the coding directly from manually running the steps in "Record Macro", but it just won't work. Hoping someone can tell me what I'm doing wrong.

Code:
Private Sub CommandButton1_Click()


Dim OpenRow As Long
Dim MDRFile As String
Dim wb1 As Workbook

OpenRow = Range("H2")
MDRFile = Range("I" & OpenRow)
    Workbooks.Open MDRFile
    Set wb1 = ActiveWorkbook
    wb1.Activate
    Sheets("Log").Select
 

'THIS IS THE CODING THAT APPEARED WHEN I CREATED THE MACRO MANUALLY GOING THROUGH THE STEPS
                ActiveWorkbook.Worksheets("Log").ListObjects("TableLog").Sort.SortFields.Clear
                ActiveWorkbook.Worksheets("Log").ListObjects("TableLog").Sort.SortFields.Add2 _
                    Key:=Range("TableLog[[#All],[Document Control '#]]"), SortOn:= _
                    xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers  ' <= THIS 3-LINE SECTION IS WHERE I HANG UP
                With ActiveWorkbook.Worksheets("Log").ListObjects("TableLog").Sort
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
         
         
'THIS IS CODING I'VE TRIED TO PUT IN WHILE SEARCHING DIFFERENT SITES FOR WHAT'S NOT WORKING.  I'VE GOT IT COMMENTED OUT RIGHT NOW
                'ActiveSheet.ListObjects("TableLog").Sort.SortFields.Clear
                'ActiveSheet.ListObjects("TableLog").Sort.SortFields.Add _
                '    Key:=Range("TableLog[[[Document Control '#]"), SortOn:=xlSortOnValues, Order:=xlAscending, Header:=Yes     ' <= THIS 2-LINE SECTION IS WHERE I HANG UP
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
One note, in case it makes a difference. My data in the sort column (Document Control #) is alpha, not numeric.
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,968
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