Control Button to initiate the writing of a new VBA macro - vba

Would it be possible to have a control button (or any mechanism) that once clicked on, a textbox would appear with fields like macro name Sub "NewMacroTitle"(), a field for the date, a field for reference sources ("Getting Links/URL from a webpage-Excel VBA"found this here"), a brief description of what the macro is to do. All of these fields, except Sub "NewMacroTitle"(), would start with a ' to show a comment and all of this would be inserted into the VBA editor, or enter it into cells on a spreadsheet that could then be copied and pasted into the VBA editor.
My problem is this. I'm new to VBA. I'm also the world's worst at documenting macros. I get an idea for a macro, I'm off into the VBA editor writing away, maybe even get the macro complete. A week later I'm reviewing the macro and the first thing is "what the heck does this thing do?"
I hope you see what I'm trying to do, essentially some way to force me to document the macro before even starting the actual code.

There are tools that will add pre-defined text to the header and/or footer of a sub. I use the one from MZ Tools for error handling code and adding my name and date to the header of each proc. It is worth a look.

Related

How to create a new custom style with specific heading

Please help as I am new to Vba and custom style in MS Word 2013.
I want to create a new custom style that provides heading as INTRODUCTION when a paragraph is been selected, and the rest of the text should come in normal style of paragraphs. Is this possible to make it in custom style or should Vba macro be used. If Vba macro is the only option then how can we add this into the particular custom file.
Styles are for formatting only, they don't contain text (such as INTRODUCTION). If I understand your request, you will need a simple macro you can write, or, actually, record.
So, let's say you have a paragraph in your document. You would like to make this paragraph have the "Normal" style. You would like to insert BEFORE this paragraph the word "INTRODUCTION", and give it the style "Heading 1".
Record the Word macro: InsertIntroduction
Position the cursor at the start of the paragraph.
Enable "Record a macro" - varies by release of Word, but, for 2010, View > Macro > Record. Be sure to specify a good name and a carefully thought out Description. Do NOT shirk this step! Put in the Description! If you are going to use it a lot, assign a keystroke to it also, that will save you a lot of time.
Type INTRODUCTION. Using cursor keys (not mouse), select it (perhaps up-arrow, Home, Shift-End). Apply style "Heading 1" (for this, you can use the mouse: Home > Style group > Heading 1 or a keystroke, usually Ctrl-Alt-1).
Arrow down to the next paragraph. Select the full paragraph using the keyboard, not the mouse (perhaps by hitting the F8 key four times, then ESC).
Apply the appropriate style (again, you can use the mouse, but the Normal style is usually linked to Ctrl-Shift-N).
You've completed the steps you wanted, based on your original request. Note that you can add a lot more, of course. But, let's say you're done.
View > Macro > Stop recording.
You've now written a program and it will be saved. Use it going forward. Cool, hunh? And you can write these things a lot faster yourself than asking others "how do I...?"
Like any other program, it can be modified to do more, or fix little errors. Let's say you misspelled INTRODuCTIoN. Tap Alt-F11 to see your program. Adjust.
9 steps seems like a lot, but, believe me, it's easier done than said. Give it a try. Do it carefully. In theory, if you make a mistake, you can simply modify the program to fix it. But, at your level of experience, you may find it easier to simply Stop recording and start over.
To Use Macro
Position cursor at start of relevant paragraph.
Execute macro. If you took my advice and assigned it to a keystroke, just tap the keystroke. If not, tap Alt-F8 to see a list of your macros. Select the one you want, and click Run.

How do you persistently format a TextBox to display a numeric value?

I'm sure this is a simple question, with a simple answer, but I can't find it.
I've inherited a spreadsheet that I have to fix. Whoever wrote it made extensive use of VBA and VBA UserForms for inputting data. On one form, textual and numeric information is entered and then saved to a record on a specific worksheet. This spreadsheet is used to log project information. It gets copied, and re-copied again and again. With each re-copy, it is cleared and used for the next project.
A user has sent me a spreadsheet of one of these major projects. The VBA, data-entry UserForm has a problem. One TextBox, which accepts either text or numbers, is always reformatting the numbers as dates when you exit the field!? I've tripled-checked the VBA code. There is no special OnEnter or OnExit code related to this field that reformats the data. Furthermore, I can't find a Format property that is associated with the TextBox from within the designer.
I'm a C/C# developer, not a VBA developer. Still, this "simple" IDE has me stumped. I can't find the property that re-formats the TextBox display value.
How can I fix the TextBox so that it persistently interprets numbers as numbers and not as dates?
FURTHER NOTE
It is worth mentioning two things. First, user's can't modify these forms or VBA code. The underlying modules are password protected and only myself and a couple managers know the password. Furthermore, no one touches the code because I'm the only developer within the company and everyone is a bit scare that they might break something.
Second, something in the file may have been corrupt. When this file was sent to me, the user also mentioned that the worksheet that he was working on was renamed. It appears that something didn't save properly because the WorkSheet tab was renamed a random hex string value.
Everything appears to be functioning as normal on this form, other than these two issues. Any help or guidance would be greatly appreciated.
You can force the proper format by using cLng:
First enter =TODAY() in cell A1 and then run:
Sub TextBoxIssue()
With ActiveSheet.Shapes("TextBox 1").TextFrame.Characters
.Text = Range("A1").Value
End With
MsgBox "However when we use cLng......"
With ActiveSheet.Shapes("TextBox 1").TextFrame.Characters
.Text = CLng(Range("A1").Value)
End With
End Sub
There is most certainly code that is formatting that text on exiting the textbox. You just haven't found it yet. If you go into the Userforms class module and select the textbox from the left dropdown, you can see all of the events in the right dropdown. The bolded events are in use. I assume you've already checked all those, but that's the place to start.
Next, look for custom class modules that use the WithEvents keyword. They can trigger events outside of the userform's class module.
Finally, search the code for all instances of =Format(tbxName.Text,"mm/dd/yyy") or some such code. Somewhere the code is probably using the Format function to fill that textbox.

VBA Macro changing macro

Is it possible to create a macro in MS Office (in this case Word) that will change other macro code? I was trying to find information but no results.
I have a doc which works as a template. Content of template is changed and then saved to another file. However it is important to have current date in it. It cannot be self-updated. Those docs go to folder of people and it is important to know when they get the document, so it must be simply data (or something that does not update).
I was thinking about an on-start event macro that would input current date and on exit it would ask "Do you want self-update functionality" Yes / No
If Yes, delete that event. However I have no idea if it is possible. If it is I still don't know how to search for it.
No this is not possible. In VBA, unlike some lower level languages when you define an event you can not disable it, even using other VBA code.
In C# or VB.NET, Java or C++ you can disable an event by un-wiring it from the handler, but this is not possible in VBA.
Maybe if you be more clear on what you need I can give you a better answer.

Standalone code for Excel

Can VBA code be written to perform actions on any Excel file?
When I create a project in Visual Studio, it asks for an Excel file to be linked to it. All the code that I write is in ThisWorkbook.vb and hence acts only on the Excel file linked to the project.
Ideally, I want a script that:
When the user double-clicks, he/she should be allowed to select an Excel file of choice for the actions to be performed on that file.
If the above is not possible, I'd at least like to invoke the VBA script from within an Excel file.
Basically, the VBA code should be independent of any Excel file.
I looked at VBScript, but the syntax for it seems to differ slightly.
You've mentioned Visual Studio, VBA, and VBScript. The solution I'm outlining works directly with VBA rather than Visual Studio or VBScript. (However, you might adapt Visual Studio (C# or VB) along the lines of what I'm outlining below.) Hope it's helpful, so here goes:
Here's what I do, and, it ultimately results in an .xlam Excel AddIn as #chris above has commented.
Overview
I start with ordinary .xslx workbook, to which I add a VBA project, making it an .xlsm. Then create some VBA Subs (macros). Then create some Excel QAT (Quick Access Toolbar) buttons for the workbook, which are bound to (i.e. they invoke) the VBA subs/macros. Then convert the workbook (with VBA in it) to an .xlam, which is an Excel AddIn. When you're all done, the buttons are accessible from any workbook (and the VBA code has access to any user workbooks as well as those originally in your .xlsm). Further, the workbook associated with the .xlam is invisible. So it just looks like you've added some buttons to the QAT that appear on all users .xlsx windows. The .xlam is pretty easy for users to install (though I provide a buttons to uninstall/reinstall/check version). You can upgrade an .xlam independently of users' workbooks; users' workbooks can thus be data only (.xlsx, no VBA).
Details
Write some Excel Subs you want to use later
You need to be aware that the buttons can only invoke macros (VBA Subs) without parameters, so the macros will have to check things like ActiveSheet and ActiveWorkbook and Selection to figure on what sheet the button was pressed, hence what user data to really operate on. (If you need to refer to your workbook with the VBA code in it, use “ThisWorkbook”). You should be aware that there can be naming conflicts, so try to name the parameterless subs with rather long names, such as MySomewhatUniqueProjectName_button1, etc…
Add Buttons to your .xlsm
Using Excel 2010 (I think this works with 2007 or later), I put workbook-specific buttons on the QAT part of the ribbon, which connect to macros (VBA subs) in the VBA code.
To do this, you from the Quick Access Toolbar customization drop down (the tiny down arrow at the very top row of the Excel window, the last icon from left to right) choose "More Commands…". When the “Customize Quick Access Toolbar” dialog box comes up, from the (2nd) "Customize Quick Access Toolbar:" heading (top to the right), choose "For XYZ.xlsm" from the dropdown instead of the "For all documents (default)". And from under "Choose Commands From:", use "Macros" (instead of “Popular Commands”) from the dropdown. Once you have those two things selected, you can move VBA subs from the left box to the right box using “Add >>”. Each so moved will become buttons visible in your QAT. As you’re doing this you can also edit the icon and text for the buttons, add a separator as needed (I always end with a separator in case other .xlam’s use the QAT). (Now is a good time to save this .xlsm.)
Convert the .xlsm into a .xlam
Then I convert the .xlsm to an Excel add-in, by merely saving it as an .xlam file. This will end up (1) hiding the workbook associated with the code you have (though it is still accessible to itself.). Further, now, (2) the (invisible, as now it's an .xlam) workbook will load whenever Excel is loaded. (To keep this fast for when users use Excel but don’t run my VBA code, I don't run any code when the .xlam is loaded, I only run code when a button is pushed.)
You can manage the AddIn using Excel’s AddIn manager. To update the AddIn, you have to use some trickery. While you can copy over it when Excel is not running, on the other hand, you cannot directly uninstall the AddIn, you can only disable it from Excel. But once disabled, you can delete the .xlam, and relaunch Excel, go to the AddIn manager to try to work with the (now gone) AddIn and you’ll get Excel saying it can’t find it, so do you want to delete it. Say yes, and it will be uninstalled.
FYI, Notes
I keep the .xlsm to edit later, but you can actually debug and edit the .xlam and later convert it back to an .xlsm with a minor bit of trickery: find its "ThisWorkbook" entry in VBA, and then the "IsAddIn" property, set to false, its workbook will suddenly appear and you can save as .xlsm, or edit its workbook and set the property back to true to resave as .xlam directly.)
Answer 1
You can do that in VB.Net too. Just make a regular project. (comment by #SLaks)
This worked for me very well and was exactly what I was looking for.
Answer 2
The very descriptive answer posted by #ErikEidt
I haven't tried this, but it seems like a good and alternative way of getting macros to work.

Excel VBA - Call macro using add in

I have added a toolbar menu for my macro, so I can just press the button and it runs my macro from any excel document. Every time I run the macro though, it opens the source file containing the macro. Is there a way that it won't open the source file and just run the macro? (even opening and closing wouldn't too much of an issue, but I'd prefer not opening it at all)
I haven't been able to find any information about this, so all help to get me started is appreciated.
You can't pull code out of the air to run it; Excel's going to have to open the file the code's stored in to run the code.
If I understand it correctly, you can create an Excel add-in from your code and have the add-in load automatically when Excel starts. Your code will always be available in that case.
This seems like a good place to start:
http://msdn.microsoft.com/en-us/library/aa140990(v=office.10).aspx
USE YOUR PERSONAL MACRO WORKBOOK
Open the VBEditor and find the module containing your macro.
Copy that code.
Now in the VBProject Panel on the left, locate your PERSONAL.XLS or PERSONAL.XLSB project. This is the project where you store macros you want available at all times. Add a module to that project and put your code into it.
Now update your "button" so that it points to the macro in that workbook and you're good to go.
On the off chance your PERSONAL.XLS project does not exist, use the macro recorder to record a "junk" macro and be sure to set it to "Store Macro In: Personal Macro Workbook"... that will create it for you the first time.