I am looking to sort An Active Worksheet/Active table. I will be using an excel file that will be growing daily and each day will have a new sheet/table. I am looking to have everything automatically setup for a different employee to do the work.
I found using ActiveSheet.ListObjects(1) will choose the active table, but sorting is causing issues.
If i use the following format it would be a one time sort, that in the end would be useless for my employee.
Sub Test()
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Add _
Key:=Range("Table1[[#All],[Client Name]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I tried the following but it didn't work.
Sub Test()
ActiveSheet.ListObjects(1).Sort.SortFields.Clear
ActiveSheet.ListObjects(1).Sort.SortFields.Add _
Key:=Range("ActiveSheet.ListObjects(1)[[#All],[Client Name]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
All codes go back to the label of the table. How can I modify this to just use what is active?
I believe you the following code will do what you expect, the main different is in the Key, I used the name property of the ListObject to get the correct name in there:
Sub Test()
ActiveSheet.ListObjects(1).Sort.SortFields.Clear
ActiveSheet.ListObjects(1).Sort.SortFields.Add _
Key:=Range(ActiveSheet.ListObjects(1).Name & "[[#All],[Client Name]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Related
My first quetion here...
I´m trying to sort fields on descending. Here is the code:
With ContEstoque.Range("Cont_Estoque_tbl").Sort
.SortFields.Add2 Key:=Range("Cont_Estoque_tbl[[#All],[DATA DA CONTAGEM]]"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ContEstoque is the sheet name.
Cont_Estoque_tbl is a formated/named table in excel.
I´m simply getting no error and no response. Any guests?
result after run the code
I found a solution by isolating everything in defined variables. Not sure exactly what was the problem, but it works.
Dim srt_rng As Range
Dim tbl_lo As Object
Set tbl_lo = ContEstoque.ListObjects("Cont_Estoque_tbl")
Set srt_rng = ContEstoque.Range("Cont_Estoque_tbl[[#All],[DATA DA CONTAGEM]]")
tbl_lo.Sort.SortFields.Clear
tbl_lo.Sort.SortFields.Add2 Key:=srt_rng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With tbl_lo.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I am trying to run the macro from sheet1 to sort the column by headers A to Z using VBA in sheet2. Anyone can help me to find it out.
Sub Macro1()
Range("C10:K13").Select
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C10:K10") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("C10:K13")
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
End Sub
As far as I know you can't sort horizontally so I transformed your range into vertical to sort and return the original value after sorting through the transpose function, this is my approach:
Sub Test()
Dim rng As Range, transposeRng As Range
Set rng = sheets("Sheet2").[C10:K10]: Set transposeRng = sheets("Sheet2").[M6].Resize(rng.Columns.Count, rng.Rows.Count)
transposeRng.Value = Application.Transpose(rng)
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add2 Key:=transposeRng.Columns(1) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange transposeRng
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
transposeRng.Clear
rng.Value = Application.Transpose(transposeRng)
End Sub
I have a macro which was running for the past number of weeks. The macro started failing. I added some with end withs to the code and solved the initial problem.
My code is now producing a "Syntax Error". Is there something I need to change in the with ?
Code
Sheets("Graph Worksheet").Select
Rows("1:1").Select
Selection.AutoFilter
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Clear
End With
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Add2 Key:= _
Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal End With
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Taken from Microsoft for With.. End With:
Executes a series of statements that repeatedly refer to a single object or structure so that the statements can use a simplified syntax when accessing members of the object or structure. When using a structure, you can only read the values of members or invoke methods, and you get an error if you try to assign values to members of a structure used in a With...End With statement.
Also:
objectExpression Required. An expression that evaluates to an object. The expression may be arbitrarily complex and is evaluated only once. The expression can evaluate to any data type, including elementary types.
In your example, the first 2 With.. End With are not needed at all, and can be omitted.
You also do not need to use .Select and should refrain from using this.
See below code:
Sub helper()
Sheets("Graph Worksheet").Rows("1:1").AutoFilter
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Add2 Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
As you will see the above compiles, but I have not tested it's use.
Solved using below code
Sheets("Graph Worksheet").Rows("1:1").AutoFilter
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Add _
Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I am in Excel I have collected a variable correctly.
Dim mysheet As String
mysheet = ActiveSheet.Name
mysheet has a value os in one case "Agent_State_Log20180320" - so this part seems right.
I am trying to use it in the following places - but my syntax is off.
ActiveWorkbook.Worksheets
(mysheet).AutoFilter.Sort.SortFields.ClearActiveWorkbook.Worksheets
(mysheet).AutoFilter.Sort.SortFields.Add Key:=Range
("C1"),SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortNormal
With ActiveWorkbook.Worksheets(mysheet).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
What should I change to get this to work correctly?
Try this
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("C1"),SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortNormal
With ActiveSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I am using VBA for Excel 2010 and randomly receiving the following error:
Run-time error '1004': "The sort reference is not valid. Make sure it's within the data you want to sort, and the first Sort By box isn't the same or blank."
This is the code
'Sort the active rows
With ActiveWorkbook.Worksheets("Product Backlog").Sort
.SetRange Range("A4:F51")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The sort by box is blank, that is your problem. I never have used a Sort object like your doing, but I can see that you have not defined a key, or a range to sort by, just the range to be sorted. A key should be defined such as Range("A4") or something. I looked it up, it should have .sortfields.add (range) in it, such as:
'Sort the active rows
With ActiveWorkbook.Worksheets("Product Backlog").Sort
.SetRange Range("A4:F51")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.SortFields.Add Key:=Range("A4:F51").Columns(1), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
.Apply
End With
I use the Sort function as follows:
ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Sort _
Key1:= ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Columns(1), _
Header:= xlYes, _
Orientation:=xlSortColumns, _
MatchCase:=False, _
SortMethod:=xlPinYin