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

I forgot to mention...

Stop using Workbook_Open events! Instead, use Excel AutoMacros...

Sub Auto_Open()
'file open code...
End Sub

Sub Auto_Close()
'file before close code...
End Sub

...in a standard module named something like "m_OpenClose". This is
where you should also make your global declarations and intialize
values at startup...

Option Explicit

Public gsAppPath$, gsNetPath$, gsSourceFile$
Public Const gsSourceFilename$ = "QueryBuster 5.21.15b.xlsm"
Public Const gsDataSheetname$ = "QueryBuster"

Sub Auto_Open()
InitGlobals
'CreateMenus
'other startup stuff
End Sub

Sub Auto_Close()
'DeleteMenus
'other shutdown cleanup
End Sub

Sub InitGlobals()
gsAppPath = ThisWorkbook.Path & "\"
gsNetPath = "\\netshare\folder\" '//edit to actual
gsSourceFile = gsNetPath & gsSourceFilename
'other initializations
End Sub

...so your code to set ref to wkbSource could be...

Set wkbSource = Workbooks.Open(gsSourceFile, UpdateLinks:=0)
Set wksSource = wkbSource.Sheets(gsDataSheetname)
Set wksTarget = ThisWorkbook.Sheets(gsDataSheetname)

...so your routine could be rewritten as...


Sub Update_QueryBuster()
' Updates ThisWorkbook.Sheets("QueryBuster")
' with data from network "QueryBuster" file.

Dim lLastRow&, wksTarget As Worksheet
Dim wkbSource As Workbook, wksSource As Worksheet

Set wksTarget = ThisWorkbook.Sheets(gsDataSheetname)

'Unfilter data in Personal QB file and rename sheet to QueryBuster1.
'Sheet will be deleted later after updated QB data is imported.
With wksTarget
If .AutoFilterMode Then .ShowAllData _
Else .Rows.Hidden = False: .Columns.Hidden = False
End With 'wksTarget

'Open main QB, copy ProView column from local QB file to main QB file.
'This will leave the sheet macro alone.
Set wkbSource = Workbooks.Open(gsSourceFilename, UpdateLinks:=0)
Set wksSource = wkbSource.Sheets(gsDataSheetname)
wksTarget.Columns("H:H").Copy
With wksSource
.Columns("H:H").Insert Shift:=xlToRight

'Find the last row and copy the formula
'in column H down from row2 to the last row
lLastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
.Range("H2:H" & lLastRow).Formula = "=I2"

'Copy all cells from main QB to local QB
wksTarget.Cells.ClearContents
.Cells.Copy wksTarget.Cells(1)
End With 'wksSource

'Close main QB
wkbSource.Close False

ErrExit:
Set wksTarget = Nothing
Set wkbSource = Nothing: Set wksSource = Nothing
If Err < 0 Then _
MsgBox "An error occured while update being processed!", vbCritical
End Sub

--
Garry

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