Anyone expert in understanding auto filter in VBA ?

nick323f

New Member
Joined
Jul 8, 2014
Messages
43
Hi all,

I need some help with confidence in the following code.
I have a data set which is given to me in the wrong order. So I need to sort the data in ascending order, by the content of column B. The data starts from row 8

Each row contains a set of measured values from an automated test of one device, with each cell containing the results of one measurement. Thus it is vital that the data in each row stays together.
The problem is that the data set has a variable number of rows, and columns, and the data set might contain empty columns and/or there might be some columns which only have data in some cells.
My fear is that, because there are blank columns and/or blank cells in the data, the auto filter and sort ascending process might occasionally pick up and sort only some of the columns. This would would mix the data meaning I could not subsequently identify which results can from which device.

I'm sorry to say that I built the following code using the auto macro recorder in Excel, because I don't have a very clear understanding of the variables in the AutoFilter command.
If anyone has a really clear understanding of this command and can see any possible way my fears could occur then can you please let me know?

Many thanks
Nick


Code:
Sub test()
'
' test Macro
'
    Sheets("Data file").Select
    Range("b7").EntireRow.Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Data file").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data file").AutoFilter.Sort.SortFields.Add Key:= _
        Range("B7"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Data file").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
    Range("A1").Select
End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
It's not clear to me how you want to use autofilter, but this example will sort the data and turn on autofilter (but not set any filter criteria)

Code:
Sub Test()    
    Dim WB As Workbook    
    Dim WS As Worksheet
    Dim SortRange As Range

    Set WB = ThisWorkbook
    Set WS = WB.Worksheets("Data file")

   'Turn off AutoFilter
    If WS.AutoFilterMode Then
        WS.AutoFilterMode = False
    End If

    'Define the range of the data to be sorted
    Set SortRange = Application.Intersect(WS.UsedRange, WS.Range("B7:B" & WS.Range("B" & WS.Rows.Count).End(xlUp).Row).EntireRow)
    Debug.Print SortRange.Address
    
    'Sort the data
    SortRange.Sort Key1:=WS.Range("B7"), Order1:=xlAscending, Header:=xlYes, DataOption1:=xlSortNormal

    'Turn on AutoFilter
    SortRange.AutoFilter
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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