PDA

View Full Version : Book2 Macros interfere with Book1 Copy Book2 Sheet



bbakker09
03-22-2016, 12:09 PM
Greetings Everyone,

I have done a lot of searching but haven't been successful at finding code to solve my problem.

My goal is this: to have a button in an otherwise empty workbook which when clicked will, open a browser window to select an excel file, select a specific sheet in the newly opened excel, copy that sheet, and paste in the originally active workbook. The issue I've run into is that the excel file I'm browsing for is a budget which itself has many macros and seems to be interrupting the other code sequences I've researched.

I have tried various codes using the Application.GetOpenFilename with copy and paste coding, but I can't seem to get the code to "wait" until the external excel is finished being opened before it copy/pastes the sheet.

My guess is that I need two separate "Sub" sections or two separate macros. One to open the excel and one to copy the data. I am making a general form for many people to use, so I can't know the specific name of the excel file I'm opening. The workbook with the button will remain fixed.

Thank you for your help.

p45cal
03-22-2016, 12:44 PM
I have tried various codes using the Application.GetOpenFilename with copy and paste coding, but I can't seem to get the code to "wait" until the external excel is finished being opened before it copy/pastes the sheet.
How about trying to prevent them from running at all?
Application.EnableEvents = False
'...
'open files etc
'...
Application.EnableEvents = True

bbakker09
03-22-2016, 01:29 PM
How about trying to prevent them from running at all?
Application.EnableEvents = False
'...
'open files etc
'...
Application.EnableEvents = True

I tried the above with no luck. I also tried "On Error Resume Next, On Error GoTo 0"

Does it help to explain that the workbook that is being opened gets a Visual Basic error " Run-time error '1004': Application-defined or object-defined error". The workbook opens, I click end on the run-time error but nothing happens to the active workbook as far as copying the worksheet.

My current code is as follows:

Dim wb As Workbook, wb2 As WorkbookDim ws As Worksheet
Dim vFile As Variant


'Set source workbook
Set wb = ActiveWorkbook


Application.EnableEvents = False
ChDrive "N"
ChDir "N:\xxx"
On Error Resume Next
vFile = Application.GetOpenFilename("Excel-files,*", _
1, "Select One File To Open", , False)
On Error GoTo 0
Application.EnableEvents = True


'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile


'Set selectedworkbook
Set wb2 = ActiveWorkbook






wb2.Range("A10:AE500").Select
Selection.Copy
wb.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.ScreenUpdating = True


wb2.Close


'Set targetworkbook
Set wb = ActiveWorkbook


End Sub

Thanks

p45cal
03-22-2016, 01:42 PM
GetOpenFilename doesn't open anything; it just get a file name. The opening of the workbook appears later:
Workbooks.Open vfile

so your enableEvents lines need to be relative to that line.