I need to save several Excel sheets to separate files.
The sheets are protected and locked, however I want to make spellcheck available.
This is possible with VBA using a small routine to unlock>spellcheck>relock
Sub SpellCheck()
ActiveSheet.Unprotect
Cells.CheckSpelling CustomDictionary:="CUSTOM.DIC", IgnoreUppercase:=False, AlwaysSuggest:=True, SpellLang:=1033
ActiveSheet.Protect
End Sub()
Now I placed command button on the sheets I want to export and assigned my spell check macro.
I save the files with vba as XLSM
Sheets("exportsheet").SaveAs Filename:="mysheet.xlsm", FileFormat:=52
If I click on the button in the newly saved file, the macro is linked to the original source excel which will open. The assigned macro link looks something like this: original_excel.xlsm!spellCheck()
How can I export a sheet including the VBA code which is assigned to a command button in a way that the macro is not assigned to the original workbook.
Any thoughts on that?
If you want the worksheet to be self contained after you export it from the workbook, make it self contained to start with.
Place all routines that are accessed from the sheet into that sheet's code module (instead of into a shared standard code module).
This way the sheet has no dependencies and will be self contained once exported to a new workbook.
Related
I am new to VBA!
I have a workbook A that I use as a template for spinoff workbooks B, C, D, etc.
I made an error in formulas range A36:E37. I need to correct it in all the subsequently created workbooks, which can have any random name
I want to open the corrected master workbook A, and copy range from A to whateverworkbookname
Every time I use thisworkbook refrerence, it pastes the data to my personal macro workbook, same thing with activeworkbook.
I'm sure there's a simple solution, (like assigning a variable to the freshly opened workbook that needs fixing?) but I don't know how to do that.
Help is much appreciated!
Also of note, I am planning on manually opening the whaverworkbookname, then VBA unprotecting the sheet, copy paste function, protecting the sheet, saving, and closing the whateverworkbookname book when the macro completes, to be repeated with the rest of the incorrect workbooks.
If there is a smarter way to do this (which is probably way over my head) like applying a macro to all workbooks in a folder for instance, I would be interested in a point in the right direction to learn about it.
First a note:
ThisWorkbook always refers to the workbook the code is written in. This never changes.
ActiveWorkbook is the workbook that has focus (is on top). This changes easily with a mouse click on any workbook.
The issue is probably that you run the code from VBA editor. But if it is in your personal workbook and you run from the editor, then as soon as you are in the code your active workbook is the personal one because that is where your code is and if you click there to run the code it has focus.
You can check if the active workbook is the personal one
If ActiveWorkbook.Name = ThisWorkbook.Name Then
MsgBox "The active workbook is the personal one. Make sure to focus on the correct workbook."
Exit Sub
End If
'rest of your code
Create a button or link a ribbon button to launch your macro and use ActiveWorkbook in your code.
I have a workbook ("CodeBook.xlsm") that runs code using a BeforeSave event. When a user has multiple workbooks open and chooses to quit excel via File/Exit, the user is prompted whether to save workbooks, and if yes to CodeBook.xlsm, then the BeforeSave code is run. The problem is, at that point the ActiveWorkbook may not be CodeBook.xlsm, unless that happens to be the workbook that the user was in when he/she selected Exit Excel. If the user quit excel from another workbook, the BeforeSave code is running but the activeworkbook is some random file of the user, so all the references to specific worksheets and ranges in the BeforeSave code do not work.
I have tried various ways using a Static declaration to retain the name of CodeBook and workbook().activate to activate it when the application is quitting, but when BeforeSave runs, it can't pick up the name CodeBook anywhere, short of hard-coding the name into the code.
Any suggestions? How to retain a variable name in memory when there is no code running, but is there when a user initiates a quit excel, OR how to activate a specific workbook when Excel is quitting from a user command and not from application.quit. Using excel 2010.
I overcame this by including a reference to the specific workbook.
For example, this code simply saves the date/time stamp in cell A1 of Sheet1 before closing the document. By adding ThisWorkbook, it works on the specific workbook that the code resides in. If you don't add ThisWorkbook then it will work on the active workbook when the user quits.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Now()
End Sub
Place this code in the ThisWorkbook module.
(Excel 2010 question)
I have a reporting application that exports To excel. I have a Macro created that does some functionality. I need to run this macro whenever I open this new Excel Workbook after I export it.
Since it's a brand new worksbook, the excel does not have the macro that I have created embedded into it. So need to know if this is possible and how to do it.
Thanks!
You can open both the workbook that contains the macro and then the NEW workbook.
Then hit F11 for the Developer's Console. Usually it opens with the Project Explorer open on the left. If it isn't you can open it in the View menu, or by hitting Ctrl+R.
You need to copy the module that contains the macro into the modules of the New Workbook. Either insert a module from the Insert menu, or right click on the Modules > Insert > Module, then copy and paste the code, or you can just INSERT the module.
Let's assume your original Macro is named "Macro1". Make sure it isn't private.
I made one for example:
Sub Macro1()
MsgBox("Welcome to the Workbook!")
End Sub
In the Project Explorer, select the New workbook, and there should be an object titled "ThisWorkbook". Right click on that and select "View Code".
Private Sub Workbook_Open()
Call Macro1
End Sub
Save it, exit, open it back up and see your macro execute.
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)
Here's my problem: I have two workbooks, one is mine, say Projet.xlsm, and the other is not, say Query.xlsm. Query.xlsm is shared between me and a lots of people in my company so I can't modify it.
My project.xlsm aims to plot some datas extracted with Query.xlsm. My job is to make it an automated process.
How Query.xlsm work: on the first sheet there is some options to select; then we have just to click a button on the first sheet. Finally all the datas are extracted and appears in second sheet.
I've built a macro in project.xlsm wich open Query.xlsm and select the options I want.
Now I'm tryi,g to "click the button" (sheet1 in Query.xlsm) from my macro in my project.xlsm. I've tried things like this: running excel macro from another workbook
But I think I have to mention the name of my sheet somewhere.
Thanks in advance
Assuming you have the workbook open (which it sounds like you do), this worked for me even on a Private CommandButton Sub.
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = True
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = vbClick
You may create a public procedure in Query.xlsm Workbook in first sheet.
cmdClick refers to the name of activex command button. Below is sample code.
Public Sub cmdClick()
CommandButton1_Click
End Sub
You can call this procedure from project.xlsm using below syntax.
Workbooks("testing.xlsm").Sheets("Sheet1").cmdClick