Error 91 in code to Export To Excel (1 Viewer)

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Good day Folks,

I have some code that exports data to an Excel spreadsheet, which I got working fine, I needed to create the same process for some other data so I created a similar process for that data. The problem I am experiencing is that whenever I run the code to export both sets of data to Excel the second created process always errors out with the error 91, having read what access pops up in the help menu, it states the following:

Object variable or With block variable not set (Error 91)



There are two steps to creating an object variable. First you must declare the object variable. Then you must assign a valid reference to the object variable using the Set statement. Similarly, a With...End With block must be initialized by executing the With statement entry point. This error has the following causes and solutions:
  • <LI class=LB1>You attempted to use an object variable that isn't yet referencing a valid object.
    Specify or respecify a reference for the object variable. For example, if the Set statement is omitted in the following code, an error would be generated on the reference to MyObject: Dim MyObject As Object ' Create object variable.Set MyObject = Sheets(1) ' Create valid object reference.MyCount = MyObject.Count ' Assign Count value to MyCount.<LI class=LB1>You attempted to use an object variable that has been set to Nothing. Set MyObject = Nothing ' Release the object.MyCount = MyObject.Count ' Make a reference to a released object.Respecify a reference for the object variable. For example, use a new Set statement to set a new reference to the object.
    <LI class=LB1>The object is a valid object, but it wasn't set because the object library in which it is described hasn't been selected in the References dialog box. Select the object library in the Add References dialog box.
    <LI class=LB1>The target of a GoTo statement is inside a With block. Don't jump into a With block. Make sure the block is initialized by executing the With statement entry point.
  • You specified a line inside a With block when you chose the Set Next Statement command. The With block must be initialized by executing the With statement.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).


however I do have the correct reference to Excel in my module which is "Microsoft Excel 11.0 Object Library, otherwise the first created code would not work. it always happens on the second created code, which has the exact same references. Here's the code and the one that is always highlighted is in red

Code:
[FONT=Times New Roman][COLOR=blue]With[/COLOR] Objsheet [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Bold = [COLOR=blue]True[/COLOR][/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Select [/FONT]
[FONT=Times New Roman]        [COLOR=red]Selection.Insert Shift:=xlDown[/COLOR][/FONT]
[FONT=Times New Roman]        Range("A1").Select[/FONT]
[FONT=Times New Roman]        ActiveCell.FormulaR1C1 = "Camera Form Code 03_Q"[/FONT]
[FONT=Times New Roman]        Range("B1").Select [/FONT]
[FONT=Times New Roman]        ActiveCell.FormulaR1C1 = "As At Date :"[/FONT]
[FONT=Times New Roman]        Range("D1").Select [/FONT]
[FONT=Times New Roman]        Selection.NumberFormat = "@"[/FONT]
[FONT=Times New Roman]        [COLOR=blue]With [/COLOR][COLOR=black]Selection[/COLOR][/FONT]
[FONT=Times New Roman]            .HorizontalAlignment = xlLeft [/FONT]
[FONT=Times New Roman]            .VerticalAlignment = xlBottom [/FONT]
[FONT=Times New Roman]            .ReadingOrder = xlContext[/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[COLOR=blue][FONT=Times New Roman]End With[/FONT][/COLOR]

Any suggestions as to why this keeps happening, because I can't been able to work out from the help information what the problem is.

Thanks in advance

John
 

boblarson

Smeghead
Local time
Today, 14:19
Joined
Jan 12, 2001
Messages
32,059
First up, post the entire code including declarations. Second, the main thing I see is that your objects are not tied to the main Excel application object. Read this to see why that is important.

So your code

Range("A1").Select

needs to be tied to the application object somehow. So, if you have a worksheet object that is instantiated as part of the app object, you can use

xlWS.Range("A1").Select

if you don't then

xlApp.ActiveSheet.Range("A1").Select

is necessary (see the link to my article which explains all of that).
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Hi boblarson,

Thanks for your response, having read reference you pointed me to, I wasn't aware of that.

So even though at the end of my code I save, close and the Quit Excel, there is still a hidden instance open until I actually close Access.

Code:
[SIZE=3][FONT=Times New Roman]ObjExcel.ActiveWorkbook.Save[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]ObjExcel.ActiveWorkbook.Close Workbook[/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3]ObjExcel.Quit [/SIZE][/FONT]

So this particular section of code looks like as shown below:

Code:
[FONT=Times New Roman][COLOR=blue]Set[/COLOR] Objsheet = ObjExcel.ActiveWorkbook.Worksheets(1) [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    [COLOR=blue]With[/COLOR] Objsheet [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Bold = [COLOR=blue]True[/COLOR][/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle[/FONT]
[FONT=Times New Roman]        .Rows("1:1").Select[/FONT]
[FONT=Times New Roman]        Selection.Insert Shift:=xlDown[/FONT]
[FONT=Times New Roman]        [COLOR=red]Range("A1").Select[/COLOR] [/FONT]
[FONT=Times New Roman]        ActiveCell.FormulaR1C1 = "Standard D & G " [/FONT]
[FONT=Times New Roman]        Range("B1").Select[/FONT]
[FONT=Times New Roman]        ActiveCell.FormulaR1C1 = "As At Date :" [/FONT]
[FONT=Times New Roman]        Range("D1").Select [/FONT]
[FONT=Times New Roman]        Selection.NumberFormat = "@"[/FONT]
[FONT=Times New Roman]        [COLOR=blue]With[/COLOR] Selection[/FONT]
[FONT=Times New Roman]            .HorizontalAlignment = xlLeft [/FONT]
[FONT=Times New Roman]            .VerticalAlignment = xlBottom [/FONT]
[FONT=Times New Roman]            .ReadingOrder = xlContext[/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]        ActiveCell.FormulaR1C1 = MyDate [/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").Select [/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").EntireColumn.AutoFit [/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").HorizontalAlignment = xlCenter [/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").VerticalAlignment = xlCenter [/FONT]
[FONT=Times New Roman] [/FONT]
[COLOR=blue][FONT=Times New Roman]End With[/FONT][/COLOR]
[FONT=Times New Roman]    [/FONT]
[FONT=Times New Roman]    ObjExcel.ActiveWorkbook.Save [/FONT]
[FONT=Times New Roman]    ObjExcel.ActiveWorkbook.Close [/FONT]
[FONT=Times New Roman]    ObjExcel.[COLOR=blue]Quit[/COLOR][/FONT]

So the bit of code I've highlighted in Red needs to look something like your suggestion.

John
 

boblarson

Smeghead
Local time
Today, 14:19
Joined
Jan 12, 2001
Messages
32,059
Your code needs quite a few more periods (somewhat hard to see here but if you copy and paste the code below into Word and make the font big enough you should see that I added a lot of periods) and also a few ObjExcel's and you can use one line to save and close as the TRUE at the end of the Close is telling it to save.

Code:
[FONT=Times New Roman][SIZE=3]Set Objsheet = ObjExcel.ActiveWorkbook.Worksheets(1) [/SIZE][/FONT]
 
[SIZE=3][FONT=Times New Roman]   With Objsheet [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       .Rows("1:1").Font.Bold = True[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       .Rows("1:1").Font.Underline = xlUnderlineStyleSingle[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       .Rows("1:1").Select[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman][COLOR=red]      ObjExcel.[/COLOR]Selection.Insert Shift:=xlDown[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]Range("A1").Select [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]ActiveCell.FormulaR1C1 = "Standard D & G " [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]      [COLOR=red].[/COLOR]Range("B1").Select[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]ActiveCell.FormulaR1C1 = "As At Date :" [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]Range("D1").Select [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red]ObjExcel.[/COLOR]Selection.NumberFormat = "@"[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       With [COLOR=red]ObjExcel.[/COLOR]Selection[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]           .HorizontalAlignment = xlLeft [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]           .VerticalAlignment = xlBottom [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]           .ReadingOrder = xlContext[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       End With[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]ActiveCell.FormulaR1C1 = MyDate [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]Columns("A:CQ").Select [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]Columns("A:CQ").EntireColumn.AutoFit [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]Columns("A:CQ").HorizontalAlignment = xlCenter [/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]       [COLOR=red].[/COLOR]Columns("A:CQ").VerticalAlignment = xlCenter [/FONT][/SIZE]
 
[FONT=Times New Roman][SIZE=3]End With[/SIZE][/FONT]
 
[SIZE=3][FONT=Times New Roman]   ObjExcel.ActiveWorkbook.Close [COLOR=red]True[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]   ObjExcel.Quit[/FONT][/SIZE]
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Good day Bob,

Thanks for your observations and recommendations, I'll let you know how I get on.

Many thanks once again

John
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Good day Bob,

I copied and pasted your suggestion into my code module [replacing the existing code] and the code stops everytime on the ".Range("A1").Select" line.

So the Excel spreadsheet is created in inserts a line at A1 and then just stops, I have to manually close the spreadsheet.

Any idea as to why this is

Code:
[FONT=Times New Roman][COLOR=blue]With[/COLOR] Objsheet [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Bold = [COLOR=blue]True[/COLOR][/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle[/FONT]
[FONT=Times New Roman]        .Rows("1:1").Select[/FONT]
[FONT=Times New Roman]        ObjExcel.Selection.Insert Shift:=xlDown[/FONT]
[FONT=Times New Roman]        [COLOR=red].Range("A1").Select [/COLOR][/FONT]
[FONT=Times New Roman]        .ActiveCell.FormulaR1C1 = "Standard D & G "[/FONT]
[FONT=Times New Roman]        .Range("B1").Select [/FONT]
[FONT=Times New Roman]        .ActiveCell.FormulaR1C1 = "As At Date :"[/FONT]
[FONT=Times New Roman]        .Range("D1").Select[/FONT]
[FONT=Times New Roman]        ObjExcel.Selection.NumberFormat = "@"[/FONT]
[FONT=Times New Roman]        [COLOR=blue]With[/COLOR] ObjExcel.Selection[/FONT]
[FONT=Times New Roman]            .HorizontalAlignment = xlLeft [/FONT]
[FONT=Times New Roman]            .VerticalAlignment = xlBottom[/FONT]
[FONT=Times New Roman]            .ReadingOrder = xlContext[/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]        .ActiveCell.FormulaR1C1 = MyDate[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").Select[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").EntireColumn.AutoFit[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").HorizontalAlignment = xlCenter[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").VerticalAlignment = xlCenter[/FONT]
[COLOR=blue][FONT=Times New Roman]End With[/FONT][/COLOR]
[COLOR=blue][FONT=Times New Roman] [/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]ObjExcel.ActiveWorkbook.Save[/FONT][/COLOR]
[FONT=Times New Roman][COLOR=black]ObjExcel.ActiveWorkbook.Close [/COLOR][COLOR=blue]True[/COLOR][COLOR=black][/COLOR][/FONT]
[COLOR=black][FONT=Times New Roman]ObjExcel.Quit[/FONT][/COLOR]

Your assistance is appreciated.

John
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Good day Bob,

I've been working on this problem, when I remove the periods from those shown in red below it goes a bit further but stops when it comes to entering the current date in Cell D1. I tried removing the periods for those in the section below, but no joy in getting any further and I have to exit Access to get the code to start afresh, so it would appear that an instance of Excel is still running.

Code:
[FONT=Times New Roman][COLOR=blue]With[/COLOR] Objsheet [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Bold = [COLOR=blue]True[/COLOR][/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle[/FONT]
[FONT=Times New Roman]        .Rows("1:1").Select[/FONT]
[FONT=Times New Roman]        ObjExcel.Selection.Insert Shift:=xlDown[/FONT]
[FONT=Times New Roman]        [COLOR=red]Range("A1").Select [/COLOR][/FONT]
[FONT=Times New Roman]        [COLOR=red]ActiveCell.FormulaR1C1 = "Standard D & G "[/COLOR][/FONT]
[COLOR=red][FONT=Times New Roman]        Range("B1").Select [/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman]        ActiveCell.FormulaR1C1 = "As At Date :"[/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman]        Range("D1").Select[/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman]        ObjExcel.Selection.NumberFormat = "@"[/FONT][/COLOR]
[FONT=Times New Roman]        [COLOR=blue]With[/COLOR] ObjExcel.Selection[/FONT]
[FONT=Times New Roman]            .HorizontalAlignment = xlLeft [/FONT]
[FONT=Times New Roman]            .VerticalAlignment = xlBottom[/FONT]
[FONT=Times New Roman]            .ReadingOrder = xlContext[/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]        .ActiveCell.FormulaR1C1 = MyDate[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").Select[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").EntireColumn.AutoFit[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").HorizontalAlignment = xlCenter[/FONT]
[FONT=Times New Roman]        .Columns("A:CQ").VerticalAlignment = xlCenter[/FONT]
[COLOR=blue][FONT=Times New Roman]End With[/FONT][/COLOR]
[COLOR=blue][FONT=Times New Roman] [/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman]ObjExcel.ActiveWorkbook.Save[/FONT][/COLOR]
[FONT=Times New Roman][COLOR=black]ObjExcel.ActiveWorkbook.Close [/COLOR][COLOR=blue]True[/COLOR][COLOR=black][/COLOR][/FONT]
[COLOR=black][FONT=Times New Roman]ObjExcel.Quit[/FONT][/COLOR]

I'll continue to see if I can find the problem.

John
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Hi Bob,

Something really strange going on here, I think that an instance of Excel is still running in the background and not closing when the code is run. I have two spreadhsheets that are created one after the other, the first spreadsheet is created as required, because I've removed the code shown in red below. However when the second spreadsheet is created it appears that the code in purple doesn't run, because no information is in the spreadsheet.

The code for the first spreadsheet for this section shown below is in brown font, and does not have the period in front and works fine, however when I did exactly the same for the second spreadsheet code in purple nothing was actioned and so I changed it back to have the periods in front and no difference nothing is actioned.

I'm at a bit of a loss as to why what has worked for the first spreadsheet is not working for the second spreadsheet. I'll keeping looking and trying different things, but I just seem to be going round in circles:

Code:
[SIZE=3][COLOR=green]'First Spreadsheet code[/COLOR][/SIZE]
 
[FONT=Times New Roman][SIZE=3][COLOR=blue]With[/COLOR] Objsheet [/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Rows("1:1").Font.Bold = [COLOR=blue]True[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Rows("1:1").Select[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        ObjExcel.Selection.Insert Shift:=xlDown[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        [COLOR=#993300]Range("A1").Select [/COLOR][/SIZE][/FONT]
[COLOR=#993300][FONT=Times New Roman][SIZE=3]        ActiveCell.FormulaR1C1 = "Standard D & G "[/SIZE][/FONT][/COLOR]
[COLOR=#993300][FONT=Times New Roman][SIZE=3]        Range("B1").Select [/SIZE][/FONT][/COLOR]
[COLOR=#993300][FONT=Times New Roman][SIZE=3]        ActiveCell.FormulaR1C1 = "As At Date :"[/SIZE][/FONT][/COLOR]
[COLOR=#993300][FONT=Times New Roman][SIZE=3]        Range("D1").Select[/SIZE][/FONT][/COLOR]
[COLOR=#993300][FONT=Times New Roman][SIZE=3]        ObjExcel.Selection.NumberFormat = "@"[/SIZE][/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3]        [COLOR=red]With ObjExcel.Selection[/COLOR][/SIZE][/FONT]
[COLOR=red][FONT=Times New Roman][SIZE=3]            .HorizontalAlignment = xlLeft [/SIZE][/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman][SIZE=3]            .VerticalAlignment = xlBottom[/SIZE][/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman][SIZE=3]            .ReadingOrder = xlContext[/SIZE][/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman][SIZE=3]        End With[/SIZE][/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3]        .ActiveCell.FormulaR1C1 = MyDate[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").Select[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").EntireColumn.AutoFit[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").HorizontalAlignment = xlCenter[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").VerticalAlignment = xlCenter[/SIZE][/FONT]
[COLOR=blue][FONT=Times New Roman][SIZE=3]End With[/SIZE][/FONT][/COLOR]
[COLOR=blue][FONT=Times New Roman] [/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3][COLOR=black]ObjExcel.ActiveWorkbook.Close [/COLOR][COLOR=blue]True[/COLOR][COLOR=black][/COLOR][/SIZE][/FONT]
[COLOR=black][FONT=Times New Roman][SIZE=3]ObjExcel.Quit[/SIZE][/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman] [/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman] [/FONT][FONT=Verdana][SIZE=3][COLOR=green]'Second Spreadhseet code[/COLOR][/SIZE][/FONT][/COLOR]
[COLOR=black][FONT=Times New Roman] [/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3][COLOR=blue]With[/COLOR] Objsheet [/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Rows("1:1").Font.Bold = [COLOR=blue]True[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Rows("1:1").Select[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        ObjExcel.Selection.Insert Shift:=xlDown[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]                [COLOR=purple].Range("A1").Select [/COLOR][/SIZE][/FONT]
[COLOR=purple][FONT=Times New Roman][SIZE=3]                .ActiveCell.FormulaR1C1 = "Camera Form Code 03_Q "[/SIZE][/FONT][/COLOR]
[COLOR=purple][FONT=Times New Roman][SIZE=3]                .Range("B1").Select [/SIZE][/FONT][/COLOR]
[COLOR=purple][FONT=Times New Roman][SIZE=3]                .ActiveCell.FormulaR1C1 = "As At Date :"[/SIZE][/FONT][/COLOR]
[COLOR=purple][FONT=Times New Roman][SIZE=3]                .Range("D1").Select[/SIZE][/FONT][/COLOR]
[COLOR=purple][FONT=Times New Roman][SIZE=3]        ObjExcel.Selection.NumberFormat = "@"[/SIZE][/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3]        [COLOR=red]With ObjExcel.Selection[/COLOR][/SIZE][/FONT]
[COLOR=red][FONT=Times New Roman][SIZE=3]            .HorizontalAlignment = xlLeft [/SIZE][/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman][SIZE=3]            .VerticalAlignment = xlBottom[/SIZE][/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman][SIZE=3]            .ReadingOrder = xlContext[/SIZE][/FONT][/COLOR]
[COLOR=red][FONT=Times New Roman][SIZE=3]        End With[/SIZE][/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3]        .ActiveCell.FormulaR1C1 = MyDate[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").Select[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").EntireColumn.AutoFit[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").HorizontalAlignment = xlCenter[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]        .Columns("A:CQ").VerticalAlignment = xlCenter[/SIZE][/FONT]
[COLOR=blue][FONT=Times New Roman][SIZE=3]End With[/SIZE][/FONT][/COLOR]
[COLOR=blue][FONT=Times New Roman][SIZE=3] [/SIZE][/FONT][/COLOR]
[FONT=Times New Roman][SIZE=3][COLOR=black]ObjExcel.ActiveWorkbook.Close [/COLOR][COLOR=blue]True[/COLOR][COLOR=black][/COLOR][/SIZE][/FONT]
[COLOR=black][FONT=Times New Roman][SIZE=3]ObjExcel.Quit[/SIZE][/FONT][/COLOR]

Your assistance is appreciated.

John
 

boblarson

Smeghead
Local time
Today, 14:19
Joined
Jan 12, 2001
Messages
32,059
Two problems here. The first is that you still have

Range("A1").Select
ActiveCell.FormulaR1C1 = "Standard D & G "
Range("B1").Select
ActiveCell.FormulaR1C1 = "As At Date :"
Range("D1").Select

Where you do not have the right references again. You need a period before all of the items there to associate them with the worksheet.

Second, you have closed the workbook and quit Excel so the second code doesn't reopen the workbook so you have the phantom Excel again. You need to save in the first code but do NOT close the workbook and do not quit the application.
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Hi Bob,

I did do as you suggested and I found that by removing the periods for that particular bit of code it then worked to a degree.

I'm not having much luck with this, can't seem to work out why's it not playing.

I created a db1.db and moved copies of the relevant tables, Queries, Macros and Modules in there. I've checked that I have the same references in my master database.

I've commented out the code that opens outlook and attaches the excel files.

There are two marcos one that targets the two spreadsheets I raised in this forum and the other marco targets a spreadsheet that is currently working okay and then moves onto these two. The db1.mdb is attached here, perhaps if you see the process itself it might help in understanding what I've done and maybe identify what I've done wrong.

Many thanks once again Bob

John
 

Attachments

  • db1.mdb
    1.1 MB · Views: 123

boblarson

Smeghead
Local time
Today, 14:19
Joined
Jan 12, 2001
Messages
32,059
Okay, I've looked through your code (seems like we could help you with those dates as it seems like you are trying to do something which can be done much simpler, but we'll save that for the moment).

One thing you should do is to declare your Excel Object as an Object and not just leave it as a variant.

Dim ObjExcel As Object

Next, the items that you have no periods next to are not connected to any instance of Excel and therefore are causing you the problem of the extra hidden Excel instance. You MUST connect them to an object, whether it be the application object, the workbook object or the worksheet object. If, by adding a period, there is an error we can work with that. Let me know what the error is and we'll try to attach it to the right object. Not all of the items connect to the application object or the workbook object or the worksheet object directly and so may need a little adjustment to get it to the right object depending on the code line you have.
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Hi Bob,

Okay will look at all that now and let you know how I get on

Thanks

John
 

boblarson

Smeghead
Local time
Today, 14:19
Joined
Jan 12, 2001
Messages
32,059
Now as far as some of the date issues go.

Why use this:
Code:
MyDate = Date
and then several lines later you use
Code:
Dim MyDateDD
Dim MyDateMM
Dim MyDateYYYY
 
MyDateDD = Format(Date, "dd")
MyDateMM = Format(Date, "mm")
MyDateYYYY = Format(Date, "yyyy")
 
MyDate = MyDateDD & "/" & MyDateMM & "/" & MyDateYYYY

And why are you splitting it out and then concatenating it all together again?
 

boblarson

Smeghead
Local time
Today, 14:19
Joined
Jan 12, 2001
Messages
32,059
And an FYI -

You can shorten your code for the formulas and the formatting down to this (so you don't have so many lines of code doing the same thing):
Code:
        With Objsheet.Range("A" & LastRow + 1 [COLOR=red]& ":BG" & LastRow + 1[/COLOR])
            'Set the formulae to Sum up all the data in the each Column
            .FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"
            'Create borders for all the columns and rows with data in them
            .Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
        .Range("A" & LastRow + 1 [COLOR=red]& ":BG" & LastRow + 1[/COLOR]).Font.ColorIndex = 3                        'Set the font in the Last Row of Column A to the colour Red
        .Range("A" & LastRow + 1 [COLOR=red]& ":BG" & LastRow + 1[/COLOR]).Font.Bold = True                           'Set the font in the Last Row of Column A to Bold
        With Objsheet.Range("A" & LastRow + 1 [COLOR=red]& ":BG" & LastRow + 1[/COLOR]).Borders                      'Get the Last Row of the column range the Totals Row
            .LineStyle = xlContinuous                                       'Create continuous lines
            .Weight = xlThick                                               'Set their weight to thick
        End With
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Good day Bob,

Thanks for your observations regarding the date aspect, I don't know what I was thinking there to be honest, and your right they serve no purpose, so I've removed them.

I'm in the process of deploying your code for the formating and summing up code that you raised on observation on, and will do some testing later this morning, and also following your observations on the subject of Excel instances I'm going over my code and making adjustments accordingly. I'll let you know how I get on.

Thanks once again for your help.

John
 

JohnLee

Registered User.
Local time
Today, 14:19
Joined
Mar 8, 2007
Messages
692
Good afternoon Bob,

I appear to have got it sorted now, Instead of trying to run the sheets one after the other and trouble shoot, because I wasn't seeing the error message, I removed the call options and followed your suggestions running the module and observing any error messages that came up and made adjustments accordingly.

Having done that for each of the modules I then coupled them together via the call functions and the process is now working as expected.

My code now looks like this:

Code:
[FONT=Times New Roman][COLOR=blue]Function[/COLOR] ExportStdDGToExcel()[/FONT]
[COLOR=blue][FONT=Times New Roman]On Error Resume Next[/FONT][/COLOR]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]DoCmd.Echo [COLOR=blue]False[/COLOR], "Running Program"     [/FONT]
[FONT=Times New Roman]DoCmd.Hourglass [COLOR=blue]True[/COLOR]  [/FONT]
[FONT=Times New Roman]DoCmd.SetWarnings [COLOR=blue]False[/COLOR] [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman][COLOR=blue]Dim[/COLOR] ExcelFile [COLOR=blue]As[/COLOR] String [/FONT]
[FONT=Times New Roman][COLOR=blue]Dim[/COLOR] ExcelWorksheet [COLOR=blue]As[/COLOR] String   [/FONT]
[FONT=Times New Roman][COLOR=blue]Dim[/COLOR] Ques [COLOR=blue]As[/COLOR] String [/FONT]
[FONT=Times New Roman][COLOR=blue]Dim[/COLOR] QueryName [COLOR=blue]As[/COLOR] String  [/FONT]
[FONT=Times New Roman][COLOR=blue]Dim[/COLOR] objDB [COLOR=blue]As[/COLOR] Database [/FONT]
[FONT=Times New Roman][COLOR=blue]Dim[/COLOR] MyDate [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]MyDate = Date                           [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    ExcelFile = "M:\Customer Satisfaction\Standard D & G\StdNewBrandDGSpreadsheets\Stddgnb" & "_" & Format(Date, "ddmmyy") & ".xls"[/FONT]
[FONT=Times New Roman]    ExcelWorksheet = "StdDGNB " & Format(Date, "ddmmyy")    [/FONT]
[FONT=Times New Roman]    Ques = "G:\eFlowStatsFrontEnd.mdb"                      [/FONT]
[FONT=Times New Roman]    QueryName = "qryStdD&GNewBrandQtrAnalysis"              [/FONT]
[FONT=Times New Roman]    [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Set[/COLOR] objDB = OpenDatabase(Ques)                          [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    [COLOR=blue]If[/COLOR] Dir(ExcelFile) <> "" [COLOR=blue]Then[/COLOR] Kill ExcelFile             [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    objDB.Execute "Select*Into[Excel 8.0;Database=" & ExcelFile & "].[" & ExcelWorksheet & "] From " & "[" & QueryName & "]"[/FONT]
[FONT=Times New Roman]    objDB.Close                                         [/FONT]
[FONT=Times New Roman]    Set objDB = [COLOR=blue]Nothing[/COLOR]                                 [/FONT]
[FONT=Times New Roman]    [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Dim[/COLOR] ObjExcel [COLOR=blue]As Object[/COLOR]  [/FONT]
[FONT=Times New Roman]    Set ObjExcel = CreateObject("Excel.Application")    [/FONT]
[FONT=Times New Roman]    ObjExcel.Visible = True                             [/FONT]
[FONT=Times New Roman]    'Open the Excel workbook Stddgnb with the current date[/FONT]
[FONT=Times New Roman]    ObjExcel.Workbooks.Open "M:\Customer Satisfaction\Standard D & G\StdNewBrandDGSpreadsheets\Stddgnb" & "_" & Format(Date, "ddmmyy") & ".xls"[/FONT]
[FONT=Times New Roman]    [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Set[/COLOR] objsheet = ObjExcel.ActiveWorkbook.Worksheets(1)        [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    [COLOR=blue]With[/COLOR] objsheet                                               [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Bold = True                           [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Font.Underline = xlUnderlineStyleSingle    [/FONT]
[FONT=Times New Roman]        .Rows("1:1").Select                                     [/FONT]
[FONT=Times New Roman]        ObjExcel.Selection.Insert Shift:=xlDown[/FONT]
[FONT=Times New Roman]            ObjExcel.Range("A1").Select                                 [/FONT]
[FONT=Times New Roman]            ObjExcel.ActiveCell.FormulaR1C1 = "Standard D & G "         [/FONT]
[FONT=Times New Roman]            ObjExcel.Range("B1").Select                                 [/FONT]
[FONT=Times New Roman]            ObjExcel.ActiveCell.FormulaR1C1 = "As At Date :"            [/FONT]
[FONT=Times New Roman]            ObjExcel.Range("D1").Select                                 [/FONT]
[FONT=Times New Roman]        ObjExcel.Selection.NumberFormat = "@"[/FONT]
[FONT=Times New Roman]        [COLOR=blue]With[/COLOR] ObjExcel.Selection[/FONT]
[FONT=Times New Roman]            .HorizontalAlignment = xlLeft                       [/FONT]
[FONT=Times New Roman]            .VerticalAlignment = xlBottom                       [/FONT]
[FONT=Times New Roman]            .ReadingOrder = xlContext[/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]            ObjExcel.ActiveCell.FormulaR1C1 = MyDate                    [/FONT]
[FONT=Times New Roman]            .Columns("A:CQ").Select                             [/FONT]
[FONT=Times New Roman]            .Columns("A:CQ").EntireColumn.AutoFit               [/FONT]
[FONT=Times New Roman]            .Columns("A:CQ").HorizontalAlignment = xlCenter     [/FONT]
[FONT=Times New Roman]            .Columns("A:CQ").VerticalAlignment = xlCenter       [/FONT]
[FONT=Times New Roman]        [/FONT]
[FONT=Times New Roman]        [COLOR=blue]Dim[/COLOR] LastRow As Long                                     [/FONT]
[FONT=Times New Roman]        [/FONT]
[FONT=Times New Roman]        LastRow = objsheet.Range("A65536").End(xlUp).Row        [/FONT]
[FONT=Times New Roman]        [/FONT]
[FONT=Times New Roman]        [COLOR=blue]With[/COLOR] objsheet.Range("A3:BG" & LastRow + 1).Borders[/FONT]
[FONT=Times New Roman]             .LineStyle = xlContinuous[/FONT]
[FONT=Times New Roman]             .Weight = xlThin[/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]        [COLOR=blue]With[/COLOR] objsheet.Range("A" & LastRow + 1 & ":CM" & LastRow + 1)[/FONT]
[FONT=Times New Roman]            .FormulaR1C1 = "=SUM(R[-" & LastRow & "]C:R[-1]C)"[/FONT]
[FONT=Times New Roman]            With objsheet.Range("A3:CM" & LastRow + 1).Borders[/FONT]
[FONT=Times New Roman]                .LineStyle = xlContinuous[/FONT]
[FONT=Times New Roman]                .Weight = xlThin[/FONT]
[FONT=Times New Roman]            [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]        .Range("A" & LastRow + 1 & ":CM" & LastRow + 1).Font.ColorIndex = 3     [/FONT]
[FONT=Times New Roman]        .Range("A" & LastRow + 1 & ":CM" & LastRow + 1).Font.Bold = True        [/FONT]
[FONT=Times New Roman]        With objsheet.Range("A" & LastRow + 1 & ":CM" & LastRow + 1).Borders    [/FONT]
[FONT=Times New Roman]            .LineStyle = xlContinuous                                           [/FONT]
[FONT=Times New Roman]            .Weight = xlThick                                                   [/FONT]
[FONT=Times New Roman]        [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman]    [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    ObjExcel.ActiveWorkbook.Close [COLOR=blue]True[/COLOR]   [/FONT]
[FONT=Times New Roman]    ObjExcel.Quit                      [/FONT]
[FONT=Times New Roman]    [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Dim[/COLOR] olApp As Outlook.Application                                        [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Dim[/COLOR] olMail As MailItem                                                  [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Set[/COLOR] olApp = New Outlook.Application                                     [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Set[/COLOR] olMail = olApp.CreateItem(olMailItem)                               [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    [/FONT]
[FONT=Times New Roman]    [COLOR=blue]With[/COLOR] olMail[/FONT]
[FONT=Times New Roman]        .To = "Statistics@domesticandgeneral.com"[/FONT]
[FONT=Times New Roman]        .CC = "John.Lee@domesticandgeneral.com"[/FONT]
[FONT=Times New Roman]        .BCC = "elaine.boulton@domesticandgeneral.com"[/FONT]
[FONT=Times New Roman]        .Subject = "Standard D&G Spreadsheet Analysis - New Branded"[/FONT]
[FONT=Times New Roman]        .Body = "Please find attached the current Calender quarter Standard D&G Excel Spreadsheet."[/FONT]
[FONT=Times New Roman]        .Attachments.Add "M:\Customer Satisfaction\Standard D & G\StdNewBrandDGSpreadsheets\Stddgnb" & "_" & Format(Date, "ddmmyy") & ".xls"[/FONT]
[FONT=Times New Roman]        .Send[/FONT]
[FONT=Times New Roman]    [COLOR=blue]End With[/COLOR][/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Set[/COLOR] olMail = Nothing    [/FONT]
[FONT=Times New Roman]    [COLOR=blue]Set[/COLOR] olApp = Nothing     [/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman][COLOR=blue]Call[/COLOR] QuarterlyExportCamera[/FONT]
[FONT=Times New Roman] [/FONT]
[FONT=Times New Roman]DoCmd.Echo [COLOR=blue]True[/COLOR], "Program End"                                              [/FONT]
[FONT=Times New Roman]DoCmd.Hourglass [COLOR=blue]False[/COLOR]       [/FONT]
[FONT=Times New Roman]DoCmd.SetWarnings [COLOR=blue]True[/COLOR]              [/FONT]
[FONT=Times New Roman] [/FONT]
[COLOR=blue][FONT=Times New Roman]End Function[/FONT][/COLOR]

and obviously the second module is constructed similarly.

I'd like to thank you for you help and time in assisting me with this, I wouldn't have got there in then end without it.

John
 

Users who are viewing this thread

Top Bottom