View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
[email protected] fdb_biz@bloomingdalecom.net is offline
external usenet poster
 
Posts: 13
Default On Open macro not running correctly

Hi. I'm trying to get the code below to run when the workbook is
opened. It runs just fine when I step through the code, but when I
put the code into the workbook to run when it's opened, it messes up.
This macro is supposed to open another workbook on our network
(QueryBuster 5.21.15b.xlsm), and then do a couple processes to update
the local workbook (QB Launcher), and then close the network workbook.
It does open the network workbook just fine, but it's supposed to copy
column H from the local workbook and insert it into the network
workbook. But it's just copying the column from the local workbook
and inserting it into itself. The macro is also supposed to then copy
cells from the network workbook and paste them into the local
workbook, and then close the network workbook. But it's closing the
local workbook instead.

Why would this macro run just fine when stepped through or as a stand
alone macro, but then mess up when put inside the Workbook Open
routine?

Thanks for any advice.

Sub UpdateQB()
'
' UpdateQB Macro
'
Dim LastRow As Long

' Turn display alerts off temporarily
Application.DisplayAlerts = False

' Unfilter data in Personal QB file and rename sheet to QueryBuster1.
Sheet will
' be deleted later after updated QB data is imported.

Sheets("QueryBuster").Select
On Error Resume Next
ActiveSheet.ShowAllData

' Open main QB, copy ProView column from local QB file to main QB
file. This will leave the sheet macro alone.
Workbooks.Open Filename:= _
"QueryBuster 5.21.15b.xlsm" _
, UpdateLinks:=0
Windows("QB Launcher.xlsm").Activate
Sheets("QueryBuster").Select
Columns("H:H").Copy
Windows("QueryBuster 5.21.15b.xlsm").Activate
Columns("H:H").Insert Shift:=xlToRight

' Finds the last row and copies the formula in column H down from row
2 to the last row

LastRow = Range("H" & Rows.Count).End(xlUp).Row
Range("H2:H" & LastRow).Formula = "=I2"
Range("A1").Select

' Copies all cells from main QB to local QB

Cells.Copy
Windows("QB Launcher.xlsm").Activate
Cells.Select
ActiveSheet.Paste

' Close main QB

Windows("QueryBuster 5.21.15b.xlsm").Activate
Sheets("QueryBuster").Select
ActiveWindow.Close

' Turn display alerts back on
Application.DisplayAlerts = True


End Sub