Monday, 2018-10-22
Hamster Online
Site menu
Section categories
Umum [15]
Komputer [36]
Kreatif [3]
Space Kosong
Adsense Indonesia
Bersepeda Yuk....
Main » Files » Ilmu » Komputer

Recording a Sort Macro in 2003 vs 2007
2012-03-06, 9:55 PM
xcel 2007 records the sort operation differently than Excel 2003. In 2007, the range is specified in the SetRange method while in 2003 the Selection object is used. Take this simple checkbook workbook.

If you record a macro to sort on check number in 2003, you get something like this

Sub Macro3()

‘ Macro3 Macro
‘ Macro recorded 2/20/2011 by Dick Kusleika

    Selection.Sort Key1:=Range("B2″), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
End Sub

Cell A1 is selected, then the current region by using the Goto – Special dialog (F5). The sort is done on the selected range. Contrast that with a macro recorded in 2007.

Sub Macro1()

‘ Macro1 Macro

    ActiveWorkbook.Worksheets("Sheet1″).Sort.SortFields.Add Key:=Range("B2:B12″) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1″).Sort
        .SetRange Range("A1:E12″)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
    End With

In this version, the range A1:E12 is hard-coded. When a new row is added, the 2003 code works the same and the 2007 code fails.

Like many readers of DDoE, I don’t accept recorded code. I only use it to discover objects, properties, and methods necessary. This is a special case. This workbook is used to instruct people who are not just new to Excel, but new to computers in general. Having them open the VBE, much less edit code, is out of the question.

I considered using Lists (Tables in 2007), but they act so differently in the two versions that I ruled it out. Ultimately I want to demonstrate recording a macro that sorts on check number and another macro that resorts on date. Any ideas on how I can accomplish this without editing the code?

Category: Komputer | Added by: hamster
Views: 432 | Downloads: 0 | Rating: 0.0/0


Total online: 1
Guests: 1
Users: 0
Berita Terkini

Link Friend

ip address
Copyright MyCorp © 2018  Create a free website with uCoz