• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 58
  • Last Modified:

personal workbook

I got help from Rgonzo https://www.experts-exchange.com/questions/28925097/

i wanted to apply this code into the workbook Open event but it did not work. i want put a code in my personal workbook in start up folder that wheneever i open any workbook, the code below is run against it.

Private Sub Workbook_Open()
    On Error Resume Next
    Cells.Rows.Ungroup
    Cells.Columns.Ungroup
    On Error GoTo 0
    If ActiveSheet.AutoFilterMode Then
       ActiveSheet.Range("A1").AutoFilter
    End If
    ActiveSheet.Cells.ClearOutline

End Sub

Open in new window

0
excelismagic
Asked:
excelismagic
  • 6
  • 4
  • 3
1 Solution
 
Rob HensonFinance AnalystCommented:
For it to happen to a Workbook when it opens it needs to be in the WorkbookOpen event of that workbook.

If you were to move that code to a standard module in your Personal Workbook, you could then trigger it manually on the other file when you open it. It would probably need tweaking to make sure it affects the file you want.
0
 
excelismagicAuthor Commented:
Thanks Rob.

but i am looking for a working solution and i am sure there is one.

the workbook open event should be able to fire the code on the "Activeworkbook"
0
 
Rob HensonFinance AnalystCommented:
The WorkbookOpen event is only triggered when the workbook containing the code is opened; it will not be triggered when another workbook is opened.

If you want the code to be available for use against any workbook then having it in your Personal workbook is the right place but it will have to be triggered manually by a button or keyboard shortcut or menu item etc.

Put this code in a standard module in your Personal Workbook:

Sub Workbook_Clear_Outline()
With ActiveWorkbook
    On Error Resume Next
    Cells.Rows.Ungroup
    Cells.Columns.Ungroup
    On Error GoTo 0
    If ActiveSheet.AutoFilterMode Then
    ActiveSheet.Range("A1").AutoFilter
    End If
    ActiveSheet.Cells.ClearOutline
End With
End Sub

Open in new window


Thanks
Rob H
1
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Rgonzo1971Commented:
Hi,

One can catch all the open events with a class module named
cAppEvents
with the code
Public WithEvents App As Application

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    'Call macro1 ' place your macro here
End Sub

Private Sub Class_Terminate()
    Set App = Nothing
End Sub

Open in new window

in a normal module
Dim moAppEventHandler As cAppEvents

Sub InitApp()
    Set moAppEventHandler = New cAppEvents
    With moAppEventHandler
        Set .App = Application
    End With
End Sub

Open in new window

in the thisWorkbook module

Private Sub Workbook_Open()
 Call InitApp
End Sub

Open in new window


you should put the code in the personal.xlsb workbook. Once it opens, all opening of excel files will fire the event

https://support.office.com/en-us/article/Copy-your-macros-to-a-Personal-Macro-Workbook-aa439b90-f836-4381-97f0-6e4c3f5ee566?omkt=en-US&ui=en-US&rs=en-US&ad=US

Regards
20161110v1.xlsm
1
 
Rob HensonFinance AnalystCommented:
Class Modules!!! That's a whole new ball game for me!
0
 
excelismagicAuthor Commented:
Thanks a million Rgonzo.

it surely worked for the msgbox.  

i replaced the message box with

Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    On Error Resume Next
    Cells.Rows.Ungroup
    Cells.Columns.Ungroup
    On Error GoTo 0
    If ActiveSheet.AutoFilterMode Then
    ActiveSheet.Range("A1").AutoFilter
    End If
    ActiveSheet.Cells.ClearOutline


End Sub

Open in new window


however when i open a file which has grouped subtotals that was inserted via DATA Outline Subtotals then it gives me error on these lines.

i get the error of Run-time error 91

Object Variable or With Block Variable not set on the lines  .

  If ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
 End If
 ActiveSheet.Cells.ClearOutline
0
 
Rgonzo1971Commented:
on which line exactly
0
 
Rgonzo1971Commented:
Could you send a dummy with the problematic outlines
0
 
excelismagicAuthor Commented:
Thanks Rgonzo.

here is the dummy file attached.

if you set up the macro in your personal workbook. try to open the attached file.  you can then see where the error pops.

basically the macro should remove subtotals and outlines on the workbook open event. but it does not, instead it throughs an error.
EE.xlsx
0
 
Rgonzo1971Commented:
cannot replicate error... runs smoothly
0
 
excelismagicAuthor Commented:
Thanks Rgonzo.

i think i have another event which is causing the error.

when i commented out the line Set xlApp = Application     then your code worked.  thanks.  is there a way that both of these will reside? i will open another question.

Private Sub Workbook_Open()

 Call InitApp


Set xlApp = Application   ' this is for use of removedocumentinformation

Application.OnKey "%{LEFT}", "UndoLastSelect" ' this is for another use

End Sub


Private Sub xlApp_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
  On Error Resume Next
xlApp.ActiveWorkbook.RemoveDocumentInformation (xlRDIAll)
  On Error GoTo 0
 'MsgBox "Removed personal stuff before saving"

End Sub

Open in new window

0
 
excelismagicAuthor Commented:
Excellent Solution as always.

thanks a million.
0
 
excelismagicAuthor Commented:
Please ignore my post 41883661. it works now.

your code all works like a charm.

thanks a million Rogonzo.

very smart solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now