Solved

personal workbook

Posted on 2016-11-10
13
29 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 31

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 31

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
 
LVL 48

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 31

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 48

Expert Comment

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

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 48

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now