[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

personal workbook

Posted on 2016-11-10
13
Medium Priority
?
48 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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 53

Expert Comment

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

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 53

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

649 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