Solved

Excel 2013 Autosort with VBA

Posted on 2014-10-30
4
346 Views
Last Modified: 2014-10-30
Hi Experts,

I want to sort me Excel 2013 Sheet Automatically when opening the workbook.

I have a couple of sheets within the workbook but I only want to auto sort the one. "All Devices"

Sheet details..
Row A4:AA4 has the Sheet Headers

Row A5:AA3003 Sheets data (to be sorted)

I want to sort all data from
A5 to AA3003
Sort Column A (Location)
And then Column B (Type)

Screenshot of manual sort attached.
manual sort data
Private Sub Workbook_open()
    
    Range("A4:AA3003").Select
        ActiveWorkbook.Worksheets("All Devices").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("All Devices").Sort.SortFields.Add Key:=Range( _
            "A4:AA3003"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("All Devices").Sort.SortFields.Add Key:=Range( _
            "B4:B3003"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("All Devices").Sort
            .SetRange Range("A4:AA3003")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
End Sub

Open in new window


Thanks
0
Comment
Question by:Dirk Mare
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40412985
Hi,

maybe by changing

on line 6

"A4:AA3003"),
to
 "A4:A3003"),

Regards
0
 
LVL 16

Author Comment

by:Dirk Mare
ID: 40413015
Thank You I can run the code from Visual Basic Application now, but it doesn't work when opening the workbook.

I think it might be this string..

Private Sub Workbook_open()

Open in new window


Thanks
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40413027
In which module is your code

it runs from the thisWorkbook module
0
 
LVL 16

Author Comment

by:Dirk Mare
ID: 40413090
Thank You its working I changed it to ThisWorkbook..
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question