Please review Access Code

jim may

Well-known Member
Joined
Jul 4, 2004
Messages
7,486
My first attempt at Automation -- Does anyone see any HOLES in this Code
When I F8 thru it - Excel DOES NOT OPEN for me to see the Final Destination Sheet; Someting is missing; But what?


Code:
Option Compare Database
Option Explicit

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\Jmay\Documents\Causey-Jack\CBF Project\Last_CBF_DB_Export.xlsx", True
On Error GoTo 0

Set myDestinationBook = appExcel.Workbooks.Open("C:\Users\Jmay\Documents\Causey-Jack\Ministers_Churches_Database_4.xlsm")
Set myDestinationSheet = myDestinationBook.Worksheets("Ministers")
Set mySourceBook = appExcel.Workbooks.Open("C:\Users\Jmay\Documents\Causey-Jack\CBF Project\Last_CBF_DB_Export.xlsx")
Set mySourceSheet = mySourceBook.Worksheets("qryDataExportedToExcel")
Set Rng1 = mySourceSheet.UsedRange
Set Rng2 = Rng1.Offset(1, 0).Resize(Rng1.Rows.Count - 1)
Rng2.Copy
myDestinationSheet.Range("A4").PasteSpecial (xlPasteValues)

Set Rng1 = Nothing
Set Rng2 = Nothing
Set myDestinationSheet = Nothing
Set mySourceSheet = Nothing
Set myDestinationBook = Nothing
Set mySourceBook = Nothing
Set appExcel = Nothing
End Sub
 
Yeah, In the immedaie window I would test for any open workbooks:
? Workbooks.Count = 2
? Workbooks(1).name, etc
then would do:
workbooks.close
then
? Workbooks.Count = 0
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Yeah, In the immedaie window I would test for any open workbooks:
? Workbooks.Count = 2
? Workbooks(1).name, etc
then would do:
workbooks.close
then
? Workbooks.Count = 0

almost - but this would only "see" the workbooks for the current application. If there were another excel instance running, it would have its own workbooks open, unknown to the other. It's hard to say but another instance would certainly be possible to have been fired up during your testing, and it might still be (have been) using that file - just as two people can open the same excel file from their PC's, but the second one only gets a read-only version.
 
Upvote 0
thanks xenou, The Task manager has become my friend - can quicky access it from Tasl Bar as I Step through the code. I will post final code today, sometimes. Tks Again!! Jim
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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