melodramatic
Board Regular
- Joined
- Apr 28, 2003
- Messages
- 187
- Office Version
- 365
- Platform
- 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.
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