|
VicM (Programmer) |
5 Apr 12 14:28 |
Have a client who has an Access DB in 2002-2003 format. They upgraded Office to 2010. But have also asked me to add functionality to the DB. In part of the new functionality I'm trying to perform an Excel Sort from within Access VBA. I keep getting a 1004, Application-defined or object-defined error. What is totally weird is that I first recorded the sort macro in Excel, then ported it into the Access VBA. The Excel Macro code works perfectly when applied standalone in Excel. Excel Macro code follows: CODESub Macro1()
Dim grows As Integer
Worksheets(2).Columns("A:A").Select grows = Worksheets(2).UsedRange.Rows.Count Worksheets(2).Range("A1:A" & grows).Select Worksheets(2).Sort.SortFields.Clear Worksheets(2).Sort.SortFields.Add Key:=Worksheets(2).Range("A1:A" & grows), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With Worksheets(2).Sort .SetRange Range("A1:A" & grows) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With
End Sub I then copy this code and insert it into the Access VBA and append the appropriate objects. That code snippet follows: CODEdim impfile as object dim grows as integer
set imfile = CreateObject("Excel.Sheet")
impfile.Application.Worksheets(2).Select grows = impfile.Application.Worksheets(2).UsedRange.Rows.Count impfile.Application.Worksheets(2).Range("A1:A" & gRows).Select impfile.Application.Worksheets(2).Sort.SortFields.Clear impfile.Application.Worksheets(2).Sort.SortFields.Add Key:=impfile.Application.Worksheets(2).Range("A1:A" & gRows), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With impfile.Application.Worksheets(2).Sort .SetRange Range("A1:A" & gRows) ' THROWS 1004 error .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Any suggestions openly welcomed! Thanks, Vic |
|