Solved

Excel 2013 Autosort with VBA

Posted on 2014-10-30
4
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

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 52

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

622 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