View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
GS[_6_] GS[_6_] is offline
external usenet poster
 
Posts: 1,182
Default On Open macro not running correctly

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!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion