VBA Run / do something when the file start up - vba

As an alternative to global variable, I am trying to read the last value I worked on at start up. I read this: Excel VBA: Workbook_Open, and this. and tried to implement my own. But it is not doing anything at the moment.
Private Sub Workbook_Open()
MsgBox "Testing.."
End Sub
I noticed when I paste the following code into my project, it is in the general, instead of in the worksheet. I don't know if that's the problem, I already have a method that listen to changed in my worksheet.
Edit: This is the screen I am at right now...
Edit: I see what happened. ****I must open view > Project Exloper > This Workbook **** in order to click on ThisWorkbook. Thank you for your help!

I think you've probably put this code in the wrong module.
It needs to go in "ThisWorkbook" section of the project.
Just double click on "ThisWorkbook" in the Project explorer window in the VBA editor and paste your code into that.

Related

Vba Macro works but not starting in main()

Have created a Macro - multiple subs, functions and forms for Solidworks.
I'm sure the code is dubious but it works when I force it to start in main()
When I add a button in Solidworks to start the macro it defaults to a different sub, which appears to be alphabetical, I get a similar behaviour starting the macro from the editor.
It appears all the subs listed are the ones with no arguments passed in.
Could anyone please guide me on why this happens? I'm sure I could frig a way around by renaming the subs, but don't realy want to.
It appears I was to impatient, again!
When adding the macro button in Solidworks you are given the choice of choosing the sub to run ... as Method

Hidden VBA Code (project locked from viewing ) denies User Form Macro

I think I am facing the same problem asked by Gaus Shaikh in the link below but I have the feeling he was not understood at that time. I try to spend some other words on this topic to help you guys for your support.
I create another question because, in my opinion Gaus question title could be misunderstood.
To recap, we have a problem dealing with an excel macro that generate a user form. Actually, if the VBA project is protected by password the macro works. The weird point is that, if I just hide the code (Visual Basic Editor-> VBA project properties -> protection -> lock project from viewing -> insert pw -> OK), the macro won't works even if the project is not protected by password.
I'm stuck here. I am not able to find a solution. Have you guys have any idea?
(I am using excel 2013)
Thank you in advance!
Gaus Shaikh question:
VBA password protected project not opening userform
N.B:
Basically, any macro which generates a user form gives " run-time error 50289. Project is protected." As an example, the following macro that generate an empty user form fails if the code is hidden and works if the code is shown.
Public Sub goUserForm()
Dim mainframe As Object
'Set main frame
Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3)
'Show the form
VBA.UserForms.Add(mainframe.Name).Show
End Sub
To fully understand the issue, try this macro on your worksheet, then hide your code. Save, close and open the document. The macro won't work unless you show your code.
Ok, now it's clearer. As the VBA project is protected you will not be able to access it even not with code. Any other code will work.
So, this will not work
Set mainframe = ThisWorkbook.VBProject.VBComponents.Add(3)
VBA.UserForms.Add(mainframe.Name).Show
Code not accessing the VBA project like this will work
UserForm2.Hide
UserForm1.Show

MS Excel User forms vba

Hi "im kind of new with excel VBA. I'm trying to do something simple as creating a USERFORM1 in VBA and showing it when workbook opens. I've looked it up online but for some reason something is not working.
I open excel, go to developer, create a userform1, add some stuff to it.
I open code for THISWORKBOOK and under Open procedure I type
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Then I save it as Macro Enabled and when i open it, nothing happens. What is going on? I know this is a silly question but am i doing something wrong?
In trust center I didn't have macros enabled. I did that and everything seems to work perfectly.

View Code When Modal Dialog Is Open In Excel

This may be a stupid question, but I feel like I'm in a pickle. I have a modal UserForm that opens when an Excel workbook is opened. When the UserForm is closed, the Excel workbook is saved and closed. I need to be able to view my code, but I can't seem to figure out how to do that because if I close the modal dialog box, the workbook closes. Does anyone know how I can view my code? I really apologize if this is a stupid question, but I can't seem to figure it out.
Thanks for you time and effort.
Without restarting the workbook i.e when the userform is shown in modal, you can use CTRL + Shift + Pause/Break to get into the VBE
Depending on laptops the key combination might change. Here is another which you can try.
Fn + Pause/Break
Hold the shift key when opening the workbook. This allows you to open office applications with macros not running and can be useful in situations like this.
Then view the macros (hit Alt+F11 to open it this editor).
The two other suggestions are good. For easiest debugging, I'd put the code that opens the userform in a separate routine and then call that routine from Workbook_Open. That way you can run and debug your code without having to re-open the workbook.
Then your ThisWorkbook module might look something like this:
Private Sub Workbook_Open()
MyUserformProcedure
End Sub
Sub MyUserformProcedure()
UserForm1.Show
End Sub
You could then comment out the line in Workbook_Open and call MyUserformProcedure, and uncomment the line when you're finished debugging.

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.