Solved

Excel 2013 Autosort with VBA

Posted on 2014-10-30
4
341 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now