Need to use a variable in choosing a Tab - excel

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

Related

range.sort.sortfields.add2 method simply give no error and no response

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

Sort a chart from first value till last row and column

I recoded a macro and then have been trying to edit the code so that it will sort the chart no matter how many values it has. I am getting an error about range of object global failed.
LastRow = Cells(Rows.Count, 17).End(xlUp).Row
LastCol = Cells(7, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("Q7", LastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("Q7", LastCol)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("Q7", Cells(LastRow, "Q")), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("Q7", Cells(LastRow, LastCol))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Sorting by specific column number - Excel VBA

The code below sort by data on a set range
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
mySheet.Sort.SortFields.Clear
mySheet.Sort.SortFields.Add Key:=Range("A2:A" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With mySheet.Sort
.SetRange Range("A1:U" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The number of columns can vary each time the code is executed. Is there a way to adapt this code so that the range uses both lastrow and lastcol?
Yes, see below adjustment (not tested), but you should get the logic.
With mySheet.Sort
.SetRange Range(mySheet.Cells(1,1), mySheet.Cells(lastrow,lastcol))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

VBA Excel Column Sorting Run Time Error 1004

I want to sort column A without using select but I keep Getting error massage,
Unable to get the sort property of the range class
'Sort Managers
With QueWS.Columns("A:A")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A2:A" & LastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Que").Sort
.SetRange Range("A1", Cells(LastRow, LastColumn))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Try this
With ActiveWorkbook.Worksheets("Que")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Que").Sort
.SetRange Range("A2:A" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With

Excel Active Sheet | Active Table | Sort

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

Resources