Exporting Problem

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following code in a module.

Public Function Excel_Export(strFileName As String)

Dim dbs As Database
Dim strMessage As String
Dim intX As Integer
Dim strSQL As String
Dim xlApp As Excel.Application
Dim xlBook_S As Excel.Workbook, xlBook_F As Excel.Workbook
Dim xlSheet_S As Excel.Worksheet, xlSheet_F As Excel.Worksheet
Dim intColumnCountS As Integer, intColumnCountF As Integer
Dim lngRowCountS As Long, lngRowCountF As Long
Dim objXL As Object
Dim objActiveWkb As Excel.Workbook

Set dbs = CurrentDb

'~~~Supplied by contributor on this site~~~
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")

'What did we find?...
If Not TypeName(objXL) = "Empty" Then
strMessage = "Excel Running."
Set objActiveWkb = objXL.Application.ActiveWorkbook
objXL.Visible = True
objActiveWkb.Close
objXL.Application.Quit
Set objActiveWkb = Nothing
Set objXL = Nothing
Else
strMessage = "Excel Not Running."
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Set xlApp = CreateObject("Excel.Application")
Set xlBook_S = xlApp.Workbooks.Open(strFileName, ReadOnly:=False)
Set xlSheet_S = xlBook_S.Worksheets(1)

xlApp.Workbooks(1).Activate 'This is better as you can specify the workbook.
xlApp.Visible = True 'Makes the MS Excel application visible.

intColumnCountS = xlSheet_S.UsedRange.Columns.Count
lngRowCountS = xlSheet_S.UsedRange.Rows.Count

For intX = 1 To intColumnCountS
xlBook_S.Worksheets(1).Columns(intX).AutoFit
Next

With xlApp
.Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Columns("AQ:AQ").Select
.Selection.Font.ColorIndex = 3
.Range("A1").Select
End With

xlApp.DisplayAlerts = False
xlBook_S.SaveAs strFileName
xlBook_S.Close SaveChanges:=True

xlApp.Application.Quit
Set xlSheet_S = Nothing
Set xlBook_S = Nothing
Set xlApp = Nothing

End Function

If I leave the bit of code within the ~~~~~ section, the With ... End With section does not work. If I delete the ~~~~~ section, the With... End With section does work but each alternative occasion i.e. the Excel application iremains open in the background and I have to delete from the task manager.

Can anyone to resolve this for me.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Here is the basic template I use when working with XL. Not sure how much it will help though.
Code:
Sub updateXl()
Dim xlApp As Excel.Application
Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim qrySetUp As Excel.QueryTable

On Error GoTo ErrorHandler

Set xlApp = GetObject(, "Excel.Application")
Set xlBook = xlApp.Workbooks.Open(strOpenMasterPath)

 ' Do things here

xlApp.Application.Visible = True

ErrorHandlerExit:
   Set rst = Nothing
   Set dbs = Nothing
   Set qrySetUp = Nothing
   Set xlBook = Nothing
   Set xlApp = Nothing
   Exit Sub

ErrorHandler:
   If Err = 429 Then
'Excel is not running; open Excel with CreateObject
      Set xlApp = CreateObject("Excel.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
      Resume ErrorHandlerExit
   End If
End Sub

Peter
 
Upvote 0
I have tied your code, but can't get it to work.

I get an error in the Error Handler section of code which says:-

Error No: 462; Description: The remote server machine does not exist or is unavailable. this error appears when the With...End With section of my code comes into play.
 
Upvote 0
Your:

With xlApp
.Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Columns("AQ:AQ").Select
.Selection.Font.ColorIndex = 3
.Range("A1").Select
End With

might work with:

With xlApp.
.Selection.Sort Key1:=xlApp.Application.[Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Columns("AQ:AQ").Select
.Selection.Font.ColorIndex = 3
.Range("A1").Select
End With

Or some such similar, I haven't tested the above but take a look at:
http://www.mrexcel.com/board2/viewtopic.php?t=20520&postdays=0&postorder=asc&start=0

I had the same problem.

Hope this works.
 
Upvote 0
Thanks Ian. That worked. Your a genius.
 
Upvote 0

Forum statistics

Threads
1,221,623
Messages
6,160,889
Members
451,676
Latest member
Assy Bissy

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