excel sort from access code

L

Legacy 15162

Guest
I am dumping data from access into an excel spreadsheet. I am looking to sort the information after this occurs, but I am getting this error.

Application-defined or object-defined error 1004

Here is my code

Private Function ExcelOuterlier()

Dim xlWB As Excel.Workbook
Dim xlApp As Excel.Application
Dim xlWS As Excel.Worksheet
Dim i As Integer

On Error GoTo ftwkshterror

Set xlApp = GetObject(, "Excel.Application")

Set xlWB = xlApp.Workbooks.Open("\\test\Reports\Outerlier Analysis\New Outerlier Report.xls")

For i = 1 To xlWB.Worksheets.Count
Set xlWS = xlWB.Worksheets(i)

If xlWS.Name = "CHAR Outerlier Report" Then
xlWS.Range("B5") = TxtBeginPeriod & " THRU " & TxtEndPeriod
With xlWS.Range("SORTCHAR")
.Sort Key1:=Range("V12"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
Else: xlWS.Name = "LA-TX Outerlier Report"
xlWS.Range("B5") = TxtBeginPeriod & " THRU " & TxtEndPeriod
With xlWS.Range("SORTLATX")
.Sort Key1:=Range("V12"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With

End If

Next i


' if not set to visible, will not show document being manipulated
xlApp.Visible = True

ftwkshterror:
Select Case Err.Number
Case 0
Case 429
Set xlApp = CreateObject("Excel.Application")
Resume Next
Case Else
MsgBox Err.Description & " " & Err.Number
End Select
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,222,071
Messages
6,163,741
Members
451,855
Latest member
mcook36155

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top