View Single Post
  #4   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

Guess I've still got a lot to learn! :) Thank you for the advice.

Frank

On Sun, 24 May 2015 15:39:51 -0400, GS wrote:

The problem with not using *fully qualified object refs* to the
workbooks/worksheets your code acts on is unexpected results that may
not be readily comprehensible. You need to declare and assign object
refs something like...

Dim wkbSource As Workbook, wkbTarget As Workbook
Dim wksSource As Worksheet, wksTarget As Worksheet
Const sSourceFile$ = "<fullpath\QueryBuster 5.21.15b.xlsm"

Set wkbSource = Workbooks.Open(Filename:=sSourceFile, UpdateLinks:=0)
Set wksSource = wkbSource.Sheets("QueryBuster")

Set wkbTarget = ThisWorkbook
Set wksTarget = wkbTarget.Sheets("QueryBuster")

..where you need to provide the FullName of the file to open. This
makes your code better self-documenting and much easier to
understand/maintain going forward!!

Optionally, you can obviate need for wkbTarget since ThisWorkbook refs
the file your code is in...

Set wksTarget = ThisWorkbook.Sheets("QueryBuster")

..so you can just *ref* the sheets in code since there's no reason to
'select' or 'activate' anything!