I want to create a macro that takes the user to the last previously active sheet. I need this macro to keep track of all previously active sheets, which it should be able to do automatically. I want to add this to an Excel add-in, since it needs to be compatible with all the worksheets that my company's using. The sheets themselves are not macro-enabled, so I can't have any code stored in the modules of ThisWorkbook.
I have tried out the examples that I've found on other websites. Unfortunately, all of them require using ThisWorkbook, which I won't be able to use. Is there a way to do this purely within an Excel add-in?
Public MyPrevSheet as String
Sub GoToPreviousSheet()
If Len(MyPrevSheet) > 0 Then
Sheets(MyPrevSheet).Activate
ActiveWorkbook.ActiveSheet.Previous
Else
MsgBox "You have not switched sheets yet since opening the file!"
End If
End Sub
Ideally, I would like for MyPrevSheet to automatically track the last active sheet in the file.
Update: I managed to make it work using Application Events. The idea is to create a class module that can then be assigned the Application.
Here are some useful links that helped me: http://www.cpearson.com/excel/AppEvent.aspx
https://www.jkp-ads.com/Articles/buildexceladdin05.asp
Related
I have a VBA add-in that sits in a shared file drive that three users in my organization have added to their Excel. I thought the add-in would be handy because I could push changes to the code without updating the code in any individual Excel sheets. When I tried updating the code in the add-in, the add-in was read-only because other users had Excel open. I've been trying to figure out how to make the add-in read only whenever anyone else (meaning anyone other than me) opens an instance of Excel. I've added the below code to a class module in my add-in. I've tried debugging, but no part of the below code is executed when a user opens a new workbook. I'm totally stuck.
Private WithEvents App As Excel.Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
If App.UserName <> "Andrew Lubrino" Then
SetAttr "C:\FilePath\Add-in.xlam", vbReadOnly
End If
End Sub
I've found a number of posts on this topic all with accepted code for seemingly the same problem, but the code simply doesn't work in my environment. Can anyone tell me what's going wrong?
edit:
I'm trying to make the add-in read only for all other users so that when I try to edit and make changes to the distributed code, I can save those changes. With the current set up, I can't save the code whenever any other user has Excel open. For the above code to work, you'd need the event to trigger for any workbook that another user opens
I've uploaded an Excel sheet to our website, but I want to be able to link to a particular row/cell within that sheet, so when people open it, they are taken to that row/cell straight away. Is this possible?
I tried using www.website.co.uk/test.xlsx#Sheet1!A7 but that doesn't appear to work.
I need to be able to link it from a Google Map comment box into a row/cell so it's leaving it fairly limited.
I would have used Google Sheets, but it's blocked by our IT department unfortunately.
As mentioned, for workbooks that are downloaded, you could try a workbook open event. Also provided users can run macros.
Option Explicit
Private Sub Workbook_Open()
If Evaluate("ISREF('Sheet1'!A1)") Then
With ThisWorkbook.Worksheets("Sheet1")
.Activate
.Range("A7").Activate
End With
End If
End Sub
The code above goes in the pane for ThisWorkbook.
I am trying to isolate the issue causing
Excel 4.0 function stored in defined names.
Not to bump an old thread, but I had this same issue as well and wanted to post my resolution in case anyone else experiences the same thing as myself.
My problem ended up being caused by a MySQL Excel COM add-on that was a component included in the MySQL Workbench package. Disabling that add-on caused the macro prompt to disappear when saving spreadsheets.
For me this message was caused by the MySQL For Excel COM Add-In. The message stopped appearing when I unloaded the COM Add-In.
The solution already pointed out is correct: disable the MySQL for Excel COM Add-in.
The reason for the message is that this add-in adds hidden names to the workbook. These names are not visible in the Excel Names Manager. But you can see them in the VBA Direct Window if you add this code to a workbook module:
Public Sub DEV_CheckNames()
Dim n As name
For Each n In ActiveWorkbook.Names
If Not n.Visible Then
Debug.Print n.NameLocal, n.RefersToLocal
'If you want to delete the name, uncomment this line:
'n.Delete
End If
Next
End Sub
Result (for a German instance of Excel 2013):
LOCAL_DATE_SEPARATOR =INDEX(ARBEITSBEREICH.ZUORDNEN(37);17)
LOCAL_DAY_FORMAT =INDEX(ARBEITSBEREICH.ZUORDNEN(37);21)
LOCAL_HOUR_FORMAT =INDEX(ARBEITSBEREICH.ZUORDNEN(37);22)
LOCAL_MINUTE_FORMAT =INDEX(ARBEITSBEREICH.ZUORDNEN(37);23)
LOCAL_MONTH_FORMAT =INDEX(ARBEITSBEREICH.ZUORDNEN(37);20)
LOCAL_MYSQL_DATE_FORMAT =WIEDERHOLEN(LOCAL_YEAR_FORMAT;4)&LOCAL_DATE_SEPARATOR&WIEDERHOLEN(LOCAL_MONTH_FORMAT;2)&LOCAL_DATE_SEPARATOR&WIEDERHOLEN(LOCAL_DAY_FORMAT;2)&" "&WIEDERHOLEN(LOCAL_HOUR_FORMAT;2)&LOCAL_TIME_SEPARATOR&WIEDERHOLEN(LOCAL_MINUTE_FORMAT;2)&LOCAL_TIME_SEPARATOR&WIEDERHOLEN(LOCAL_SECOND_FORMAT;2)
LOCAL_SECOND_FORMAT =INDEX(ARBEITSBEREICH.ZUORDNEN(37);24)
LOCAL_TIME_SEPARATOR =INDEX(ARBEITSBEREICH.ZUORDNEN(37);18)
LOCAL_YEAR_FORMAT =INDEX(ARBEITSBEREICH.ZUORDNEN(37);19)
These names are added to the active workbook in the moment you click on the button of the add-in (which opens the taskpane). Unfortunately, I haven't found out yet why the MySQL add-in sometimes adds these names by itself without being activated.
Added: This is a known bug in the MySQL for Excel add-in as of http://bugs.mysql.com/bug.php?id=73467
I have the same problem in Excel 2013 and was solved by unloading the MySQL Add in - I did this by going to FILE - OPTIONS - Add Ins
I ran into this warning after creating the following defined name:
Name: wsNamesArray
Refers To: =RIGHT(GET.WORKBOOK(1),LEN(GET.WORKBOOK(1))-FIND("]",GET.WORKBOOK(1)))
This formula returns an array with names for each sheet in the workbook. I was using it to return the names of various sheets (first, last, previous and next sheet).
The problem is that this formula uses the GET.WORKBOOK Excel 4 function. From what I understand this is something like a VBA macro imbedded in a function. To save that functionality the Excel workbook must be saved in a format that allows macros such as .xlsm or .xlsb (or any of several other file types).
My choices were to delete the defined name containing an Excel 4 function or to replace the defined name with a formula without the Excel 4 function,
As in ChipsLetten's answer, same solution here on my computer: On saving some (actually macro free) workbook, I get an alert "Excel 4.0 function stored in defined names", and an option to save the workbook as "macro-enabled". On de-activating the "MySQL for Excel" COM-Add-in, the alert does not appear any more.
In order to disable the "MySQL for Excel" add-in (if you ever have installed it): Tab "Developper", then button "COM Add-Ins", and you get a list of add-ins that you can enable or disable per tick-mark.
have you installed some connector for excel?, maybe that's causing the issue, just uninstall it and the error will be gone.
If you have worksheets with hidden ranges as described in domke consulting's answer, you can use the following code to remove them:
Public Sub Remove_Hidden_MySQL_Names()
Dim n As Name
For Each n In ActiveWorkbook.Names
If Not n.Visible Then
'Delete Names added by MySQL for Excel add-in
If (InStr(n.NameLocal, "LOCAL_") <> 0 And (InStr(n.NameLocal, "_FORMAT") <> 0 Or _
InStr(n.NameLocal, "_SEPARATOR") <> 0)) Then
Debug.Print "Would delete", n.NameLocal, n.RefersToLocal
'If you want to delete the name, uncomment this line:
'n.Delete
Else
Debug.Print "Keeping", n.NameLocal, n.RefersToLocal
End If
End If
Next
End Sub
For me, in 2020, this issue is caused due to the fact that you want to save it into a .xlsx format. It needs to be saved in a .xls .xlsm or .xlsb file, which are macro enabled files. .xlsm is the preferred option.
I want to create an excel template that will includes formulas for dating the columns. However, since those formulas will be based on TODAY(), I need to convert them to static strings (so the dates don't change everytime someone opens it). Is there a way to add a macro that will run automatically when someone creates a new spreadsheet based on the template? (Similarly to Auto_Open(), only on Create, rather than Open). If so, I could just create a macro that will replace the formulas with their results upon document creation. Can this be done?
[Note: I'm not married to this solution; it just seemed like the simplest way to protect my spreadsheet. If someone can suggest an alternative approach, I'd be obliged.]
I have a couple thoughts...
If you run a copy/paste values macro every time it really won't matter, right?
You could check if the file exists yet (has been saved), and if not
then this must be the template opened as a new workbook, maybe?
Code:
Private Sub Workbook_Open()
If Dir(ActiveWorkbook.Name) = "" Then
'run the macro?
MsgBox "I'm gonna run this macro"
End If
End Sub
You could have a cell on one of the sheets, that will never be used,
or is hidden, that will store whether or not to run the macro, and
change that when the file is opened or when the macro is ran. Then
have a macro run on open that checks that cell. (Or custom/document property)
You could populate the cells that have the today() formula only on
open and if they are already populated then don't run the macro?
I realized that there is no need for a Workbook_Create() function, as that behavior can be emulated by simply deleting the macro after it has run once (which happens when it is first created). Deleting macros is done automatically when the file is saved with a .xlsx extension. In addition, you need to prevent the macro from running when you open the template itself (while editing the it). You can hold the SHIFT key when opening it to prevent auto-run macros, but since that method isn't foolproof, I added this code at the top:
Private Sub Workbook_Open()
'If we're opening the template, don't run the macro
If Application.ActiveWorkbook.FileFormat = xlOpenXMLTemplateMacroEnabled Then
Exit Sub
End If
...
'add code here to SaveAs .xlsx, thus removing the macros, so it won't run every time.
End Sub
(Note: I didn't show my SaveAs code as it is rather messy: I wanted to suppress the default warning about losing macros, but wanted to also protect the user from inadvertantly overwriting previous file. If anyone is interested, I could post it)
I've got a VBA macro in an Excel 2003 spreadsheet and I'd like to 'install' it on a machine running Excel.
I created a 'Trusted Location' on the local machine, and I know how to copy the module to the existing workbook and assign a key combination to invoke it, but I don't know how to make the macro appear automatically when someone starts Excel. Help?
The simplest solution is ( both for xl2003 and xl2007 ) to copy your xls containing the macro to the path = Application.StartupPath from your client machine ( can build a simple vbscript installer which instantiates excel and retrieves this information ).
This way your macro will be available in any workbook opened since the xls files located in startuppath are loaded at excel startup.
I remember the old way was to save your macro in your Personal.xls
workbook - then it would be accessible
any time you opened Excel - has this changed for 2007?
That was a way. A better way was (and is) to create an Add-in, which you can then enable & disable via Tools->Add-ins. (An Add-in will remain enabled even if you close and re-start Excel). You can save any .XLS file as an Add-in (.XLA).
Within your Add-in you could just use an Auto_Open method:
Private Sub Auto_Open()
DoStuff
End Sub
...or, you could hook up the Workbook_Open event, as Ryan suggests.
Of course, since the user can disable the Add-in, you also want to do the reverse in Auto_Close (or in the Workbook_BeforeClose event).
Actually, if you use events it'd be better to use the Workbook_AddinInstall and Workbook_AddinUninstall events. Using those is slightly more "correct", and also has the benefit that the 'close' event doesn't fire if you close Excel and then hit Cancel when prompted to save.
I'm not sure what you mean by 'appear', but you can execute it automatically by calling it from
Private Sub Workbook_Open()
End Sub
in the ThisWorkbook object module. Is that what you're looking for?
I remember the old way was to save your macro in your Personal.xls workbook - then it would be accessible any time you opened Excel - has this changed for 2007?