Hello everybody!
I'm trying to sort the column data of a sheet with VBA using AutoFilter, taking the worksheet in the exercise image.
When the data started at A1, the code I posted below the image sorted normally, in the order I wanted. But when I changed the base start to B2, I changed the code to suit, including the AutoFilter.Sort parameters.
And to my surprise, the columns are still sorted as if the initial column were column A. That is, if the code is configured to sort the data in column B, where the base starts, it is column C that is affected .
Could you help me understand why this happens, and how to solve it?
I'll be waiting. Thank you for your consideration. : - D
I'm trying to sort the column data of a sheet with VBA using AutoFilter, taking the worksheet in the exercise image.
When the data started at A1, the code I posted below the image sorted normally, in the order I wanted. But when I changed the base start to B2, I changed the code to suit, including the AutoFilter.Sort parameters.
And to my surprise, the columns are still sorted as if the initial column were column A. That is, if the code is configured to sort the data in column B, where the base starts, it is column C that is affected .
Could you help me understand why this happens, and how to solve it?
VBA Code:
Sub F_Ribeiro
Dim wBase As Worksheet
Dim headerColumns As Range
Set wBase = Worksheets("Base")
Set headerColumns = Range(Cells(2, 2), Cells(2, Columns.Count).End(xlToLeft))
wBase.Activate
If wBase.AutoFilterMode = True Then
wBase.AutoFilterMode = False
End If
With headerColumns.CurrentRegion
.AutoFilter
.Sort Key1:=.Range("B2"), Order1:=xlAscending, Header:=xlYes, Key2:=.RangeI("C2"), Order2:=xlAscending
End With
End Sub
I'll be waiting. Thank you for your consideration. : - D