Just reveiwing some code..

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
I'm just reveiwing some code in A Standard ACCESS Module, as follows...
and I'm unclear as to:

1) appExcel.Visible = True << What is this doing?
2) Set myDestinationBook = appExcel.Workbooks.Open << I suppose this opens the File referred to in the path which follows (below). Does this Assume that Excel is not previously Open and Running and It Loads it with this one instance? What if it were already Oen before running this? Would there be a problem with the code..?


Sub ExportToMyExcel()
Dim appExcel As New Excel.Application
Dim mySourceBook As Excel.Workbook
Dim myDestinationBook As Excel.Workbook
Dim mySourceSheet As Excel.Worksheet
Dim myDestinationSheet As Excel.Worksheet
Dim Rng1 As Range
Dim Rng2 As Range
'On Error Resume Next

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryDataExportedToExcel", _
"C:\Users\James\Documents\Trusted_Locations\CBF\Last_CBF_DB_Export.xlsx", True

appExcel.Visible = True
'appExcel.ScreenUpdating = False
Set myDestinationBook = appExcel.Workbooks.Open(FileName:="C:\Users\James\Documents\Trusted_Locations\CBF\Ministers_Churches_Database_4.xlsm", ReadOnly:=True)
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
1) appExcel.Visible = True << What is this doing?
When you instantiate a new instance of Excel, it is instantiated with the .Visible property set to FALSE, so you can't see it. So in order to see it you have to set the visible property to True.
2) Set myDestinationBook = appExcel.Workbooks.Open << I suppose this opens the File referred to in the path which follows (below). Does this Assume that Excel is not previously Open and Running and It Loads it with this one instance? What if it were already Oen before running this? Would there be a problem with the code..?
It is instantiating the workbook object that has been declared. appExcel has already opened up a new instance of Excel but the workbook object needs to be instantiated as well. So this code instantiates it (you can tell when something is being instantiated because you will see the SET keyword) and then it sets itself to be the workbook that has the file name and opening characteristics as provided in the next part of the code (which goes with the Workbooks.Open part ).

If Excel isn't previously running the
Dim appExcel As New Excel.Application

takes care of that.

If the workbook is already opened when this code runs it could have a problem but it would likely open as read only since another user (or yourself) already has it open.

Hopefully that helps.
 
Upvote 0
Actually, the remainder of the code (omitted in Post1) is (FWIW):


Set myDestinationSheet = myDestinationBook.Worksheets("Ministers")
With myDestinationSheet
.Range("A4:Q5000").ClearContents
End With
Set mySourceBook = appExcel.Workbooks.Open("C:\Users\James\Documents\Trusted_Locations\CBF\Last_CBF_DB_Export.xlsx")

Set mySourceSheet = mySourceBook.Worksheets(1)
Set Rng1 = mySourceSheet.UsedRange
Set Rng2 = Rng1.Offset(1, 0).Resize(Rng1.Rows.Count - 1)
Rng2.Copy
myDestinationSheet.Range("A4").PasteSpecial (xlPasteValues)
mySourceBook.Close SaveChanges:=False

With myDestinationSheet
.Range("E1").Interior.ColorIndex = 6
.Range("G1").Interior.ColorIndex = 6
.Range("D4").Select
End With
appExcel.ScreenUpdating = True
Set Rng1 = Nothing
Set Rng2 = Nothing
Set myDestinationSheet = Nothing
Set mySourceSheet = Nothing
Set myDestinationBook = Nothing
Set mySourceBook = Nothing
Set appExcel = Nothing
End Sub
 
Upvote 0
Thanks Bob, appreciate you comments. Do you see anything drasticly wrong with the remainder of the code (except I now see a Screenupdating = True, without a previous Scrennupdating = False...

Thanks Again,

Jim
 
Upvote 0
Setting the worksheet and workbook variables to nothing isn't really necessary as they will be destroyed when they go out of scope when the appExcel is set to nothing.

Also there is this up top:
Code:
With myDestinationSheet
.Range("A4:Q5000").ClearContents
End With
but if you remove the End With and then just leave that down at the bottom where it is now again, you can simplify this one:

Code:
myDestinationSheet.Range("A4").PasteSpecial (xlPasteValues)
to this:
Code:
   .Range("A4").PasteSpecial (xlPasteValues)
As long as you also remove the
Code:
With myDestinationSheet
which falls just before this part:
Code:
Set Rng1 = Nothing
Set Rng2 = Nothing

And personally I would change from Early Binding (where a reference is set and different code is used to instantiate the objects) to Late Binding so it doesn't matter which version of Excel you are using.

So, revisions like this:
Code:
Sub ExportToMyExcel()
    Dim appExcel As Object
    Dim mySourceBook As Object
    Dim myDestinationBook As Object
    Dim mySourceSheet As Object
    Dim myDestinationSheet As Object
    Dim Rng1 As Object
    Dim Rng2 As Object
 
    On Error GoTo Errors
 
    Set appExcel = CreateObject("Excel.Application")
 
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "qryDataExportedToExcel", _
                              "C:\Users\James\Documents\Trusted_Locations\CBF\Last_CBF_DB_Export.xlsx", True
 
    'appExcel.Visible = True  ' Set this here when debugging but to make it go faster don't do it until the end
 
    Set myDestinationBook = appExcel.Workbooks.Open(FileName:="C:\Users\James\Documents\Trusted_Locations\CBF\Ministers_Churches_Database_4.xlsm", ReadOnly:=True)
 
    Set myDestinationSheet = myDestinationBook.Worksheets("Ministers")
 
    With myDestinationSheet
        .Range("A4:Q5000").ClearContents
 
        Set mySourceBook = appExcel.Workbooks.Open("C:\Users\James\Documents\Trusted_Locations\CBF\Last_CBF_DB_Export.xlsx")
 
        Set mySourceSheet = mySourceBook.Worksheets(1)
        Set Rng1 = mySourceSheet.UsedRange
        Set Rng2 = Rng1.Offset(1, 0).Resize(Rng1.Rows.Count - 1)
        Rng2.Copy
 
        myDestinationSheet.Range("A4").PasteSpecial (xlPasteValues)
 
        mySourceBook.Close SaveChanges:=False
 
        .Range("E1").Interior.ColorIndex = 6
        .Range("G1").Interior.ColorIndex = 6
        .Range("D4").Select
 
    End With
 
    appExcel.Visible = True
 
    ' this leaves the workbook and application open for the user but
    ' we can destroy our variable
    appExcel.UserControl = True
 
    Set appExcel = Nothing
 
ExitHere:
    Exit Sub
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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