Solved

personal workbook

Posted on 2016-11-10
13
40 Views
Last Modified: 2016-11-11
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
Comment
Question by:excelismagic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41882277
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
 
LVL 3

Author Comment

by:excelismagic
ID: 41882312
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
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41882343
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41882478
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
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41882489
Class Modules!!! That's a whole new ball game for me!
0
 
LVL 3

Author Comment

by:excelismagic
ID: 41882613
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
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41882630
on which line exactly
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41882633
Could you send a dummy with the problematic outlines
0
 
LVL 3

Author Comment

by:excelismagic
ID: 41883450
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
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41883456
cannot replicate error... runs smoothly
0
 
LVL 3

Author Comment

by:excelismagic
ID: 41883661
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
 
LVL 3

Author Closing Comment

by:excelismagic
ID: 41883662
Excellent Solution as always.

thanks a million.
0
 
LVL 3

Author Comment

by:excelismagic
ID: 41883672
Please ignore my post 41883661. it works now.

your code all works like a charm.

thanks a million Rogonzo.

very smart solution.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vba search, delete, and enter 0 for entire sheet row A 13 29
Excel Data Validation Help 9 49
Macro problems with Excel file 6 27
Look for an number in a column 42 24
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question