MS Excel User forms vba - 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.

Related

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

Creating private intance of excel for app

I am making an app using Excel, with all user interaction happening in userforms. I would like my app to run in a separate instance of excel than any other file already opened, or any file opened manualy by the user while my app runs. I found the folowing link to be relevant and interesting but I am not sure how to implement this in my application: Answers.microsoft.com Can I create an instance of Excel that can only be accessed by my VBA project?
Here is how I would like to implement it:
I use two files: StartFile.xlsb, SystemFile.xlsb
The user opens StartFile normaly by clicking on the file
Sheet1 of StartFile displays a warning if macros are not enabled.
Once macros are enabled, StartFile checks excel version etc...
If all is good StartFile then opens SystemFile in a new instance of excel, and closes itself
SystemFile opens and somehow makes its excel instance private (Using "Application.IgnoreRemoteRequests = True" ?)
Clarification: The user will not be able to interact with the private excel instance application at all. Its .visible setting will remain false. It is used to display userforms, so to the user this will look like a standalone app
Sidenote: How do I make sure my new instance of excel has macros enabled no matter the macro security settings? (the user has already enabled macros once) (I have not noticed this happening so far, could it?)
Is it something like this you are looking for?
You cannot change the macro security settings as that would undermine the whole purpose of having these settings. Alternative, you can properly search for a VBA module to changing the Windows registry for this.
Furthermore, be careful if you use IgnoreRemoteRequeststhen you have to reset it again everytime.
** Edited ** Hide the SystemFile workbook and just show the userform? And then close the SystemFile workbook when the userform is closed.
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
'UserForm module
Private Sub CommandButton1_Click()
Unload UserForm1
Application.Quit
End Sub

Progress Bar in Excel using VBA and userForms

so I've been looking and cant find anything on this, I hope someone can help.
what I am trying to do:
I want to create a progress bar for my excel workbook that I am making. in order to accomplish this I have created a user form with data fields that I can manipulate from outside of the userform. what I would like to do is to load the userform from inside a module and then from the same module update the userform as the module continues to run.
is there a way to do this?
currently when I use userForm1.Show it displays the userform, but the control never goes back to the calling module, and the code ends when reaching the End Sub for Private Sub userForm1_Activate()
any help would be appreciated.
thank you
You are opening the userform as a modal form. To return the control back to the calling module, you must open the userform modelessly:
UserForm1.Show vbModeless

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.

VBA Run / do something when the file start up

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.