Update:
If I defined range as input_range from A2:P29. Then If Enter in row 30, then it works. However, the Input_range is not automatically extended and I would like to shift the whole row down not only last row in the range. How could I achieve these two things?
Previous question
Starting with the help of Siddharth Rout, I am able to make this work on new Excel file. However, no matter what I tried on this file
Sample Excel File - it just does not work. I guess there is some kind of structure to prevent from automating adding row. If so, how can I am able to make the code work?
When you're detecting the Worksheet_Change() event, you need to have your code in the worksheet object, not in a module.
Here's how to do it:
Alt-F11 to open the VBA editor
Copy the Worksheet_Change() sub from Module1
Double-click on Sheet6(itemmaster)
Paste the code in there
Make sure you see Worksheet and Change in the two drop-downs at the top of the editor.
Note: you should always have Option Explicit turned on to require variable declaration. Read this answer to see how to do it: https://stackoverflow.com/a/10653204/138938
Related
I need to change Print Settings to Print Active Sheets instead of Print Selection.
I tried using macro recorder, but it did not capture any code.
Also, this simple vba code just print the active sheet, but it does not change print settings as required at run time.
ActiveSheet.PrintOut
In advance, your help is appreciated.
The setting in discussion is workbook specific. You cannot change it globally even using another language, because of the previous mentioned truth. I mean, using a specific language method.
But you can do it, using VBA, simple running a piece of code like the following one printing two sheets. Next time, for that specific workbook the setting will be changed. You obtain what you need when the code runs and also change the setting:
Sub printActiveSheets()
Dim wb As Workbook
Set wb = ActiveWorkbook
Sheets(Array(wb.Sheets(1).name, wb.Sheets(2).name)).PrintOut
End Sub
It will print first and second sheet and will also make the change you need.
In order to check that, please manually print a selection, then look at that specific setting. It will be changed in 'Print Selection`. Then, run the above code and make the same check. You will see 'Print Active Sheets'. It will also be memorized for that specific workbook if you save it.
It is not easy to change that setting, let us say, As Default, being valid for all open workbooks. I cannot swear that it is impossible. If I will take it like a challenge I will maybe find some Registry keys able to be handled (as Excel does) in order to do it for all involved workbooks. But the effort cannot be covered by the result, I think. This is the way Excel works, should be the essence of the answer, I also think.
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 downloaded this macro to apply names to worksheets (worth noting that this page is also referenced in Microsoft's help pages somewhere). But when I try to run the macro, it's like it doesn't exist. If I select Macros from the Developer tab, it's not there (and yes, I've checked "all open workbooks"). If I open the VB editor window and open the module, I can see the code, but selecting Run or Debug
F8 does nothing at all. No error, no highlighted code. All works as expected for all my other macros.
This is Excel2010 on Windows7Enterprise.
Any ideas?
You do not run this macro. It goes into the Worksheet_Change event which is the specific Worksheet object module (ie. Sheet1 object module, Sheet2 object module, etc).
It is an event that fires automatically when you modify a cell in worksheet.
There instructions how to run this are actually on that web so it's worth reading them! :)
This is where the code should be placed
You can read more about the Worksheet_Change() event here
Just in case you still wander how to run this
place the code in the Sheet1 object module code area
go back to your spreadsheet Sheet1
type in a name in cell A1, and your sheet1 name will immediately change to whatever you've typed as long as it doesn't cause macro to exit at some point. The restrictions are mentioned here
I am attempting to make a custom hotkey that will go to the previous sheet viewed.
As far as I understand, this would involve making a Class Module as a Worksheet object that will capture Sheet Change, Sheet Activate, and Sheet Deactivate Events. Once the event has been captured, I would set the previous active worksheet to an object variable. Then I would make a custom macro that would go to the previously set worksheet object once a hotkey is pressed. Is this the right path?
And if it is the right path, where would I save the Macro code so that it's usable by all future workbooks? I have a PERSONAL.xlsb file created and ready to edit. Would I make the class module in PERSONAL.xlsb? How would I initialize the object in PERSONAL.xlsb when I create a new workbook?
Answers to these questions would be greatly appreciated, thank you!
Generally on the right line, but:
you only need the Deactivate event to capture the last sheet
to code this for all workbooks, you would use an Application level event handler. See cPearson site
you may want to handle WorkbookDeactivate as well
I would create a class event handler (as described in the link) including a property for the last sheet, and a Module level Sub to get the LastSheet from the class, and activate it
I have the following VBA code, which should show a dataform from another hidden sheet.
Sub CoverageBssEntry()
Application.ScreenUpdating = False
Sheets("myhiddensheet").Select
Range("myTable[#All]").Select
ActiveSheet.ShowDataForm
End Sub
When I run this, the data form does not containt the labels and inputboxes of this table.
Any help is really appreciated, because it is driving me nuts! My only other option is to spend time to build custom made user forms, while this would do perfectly.
You cannot select a hidden sheet. And anyway the .Select statements are not necessary
Try
Sheets("myhiddensheet").ShowDataForm
The fix is to use:
ActiveSheet.Cells(x,y).Select
prior calling the .ShowDataForm, works like a jiffy!
I think there are genuine constraints with the ShowDataForm command. It works fine if invoked outside of VBA while in a specific range. But once coded into VBA, it will only return the form for the first table in the referenced worksheet, even if a macro was recorded to perform that action.
I cannot tell why. Maybe because the showdataform event is tied to the worksheet and not to the table or list selected when it is called. Sorry guys. Maybe microsoft will improve this in the future.