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
Solved

personal workbook

Posted on 2016-11-10
13
38 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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