Questions tagged [userform]

A customizable GUI that interfaces easily with VBA when working with Microsoft Office products.

Userforms can be a critical part of Microsoft Office programming, especially in Word and Excel, in that they provide a surface with which you can interact with a user and that is totally under your control, making it possible to build more complex VBA applications.

Userforms can be used:

  • To collect and verify information from a user before it's entered into a worksheet or document.
  • To prompt a user with information, while allowing for customizable responses.
  • As part of an add-in to display options and control execution of a particular automation task.

Whereas a standard MsgBox or InputBox can perform some of these tasks, userforms are more robust than these options as they allow for a much higher degree of customization. For example, window size or color, user input, and code that runs within the form itself. In other words, and entire application may be built into a single userform, rather than just leaving users to click [OK].

2576 questions
26
votes
1 answer

Are there disadvantages in putting code into Userforms instead of modules?

Are there disadvantages in putting code into a VBA Userform instead of into a "normal" module? This might be a simple question but I have not found a conclusive answer to it while searching the web and stackoverflow. Background: I am developing a…
23
votes
2 answers

Passing variable from Form to Module in VBA

I have the following button on a Form: Private Sub CommandButton1_Click() Dim pass As String pass = UserForm1.TextBox1 Unload UserForm1 End Sub I then have a Module called Module1: Public Sub Login() ... UserForm1.Show …
JimmyK
  • 4,315
  • 7
  • 29
  • 42
19
votes
1 answer

Differences between Excel's Form Controls & ActiveX Controls

Why are there 2 types of controls available in Excel? (2 buttons, 2 combo boxes, 2 check box, etc...) What's the difference between Forms Controls and ActiveX Controls? Which one should I use? Some code samples I find online work with my…
ashleedawg
  • 17,207
  • 5
  • 53
  • 80
14
votes
3 answers

Adding controls to a frame in an Excel userform with VBA

I need to create labels and buttons dynamically and then add them to a frame within a userform. How do I do this? Seems like it should be easier than it really is.
notnot
  • 4,163
  • 11
  • 41
  • 54
13
votes
3 answers

VBA - destroy a modeless UserForm instance properly

Intro: I am aware that - showing UserForms - it's best practice to handle QueryClose within the userform code (If CloseMode = vbFormControlMenu ...) doing no Unload Me therein, just a timid Me.Hide instruction (after preventing [x]-itting and…
T.M.
  • 6,659
  • 3
  • 24
  • 42
12
votes
6 answers

Remove Dynamically Added Controls from Userform

I have an Excel userform with dynamically added checkboxes. I add the checkboxes with code that looks like this: Set chkBox = Me.Controls.Add("Forms.Checkbox.1", "Checkbox" & i) I want to remove all of these checkboxes. Dim j As Integer 'Remove all…
ale10ander
  • 862
  • 4
  • 23
  • 39
12
votes
6 answers

Hide close [X] button on excel vba userform for my progress bar

I created a userform to show a progress bar when the macro is still importing sheets The problem is the user can press the red [X] button that will close and interrupt the processing done. Is there a way to hide this red button of doom so that…
forums
  • 447
  • 2
  • 5
  • 18
11
votes
3 answers

VBA drag and drop file to user form to get filename and path

I'd like to learn a new trick, but I'm not 100% confident it is possible in VBA, but I thought I'd check with the gurus here. What I'd like to do is eschew the good-old getopenfilename or browser window (it has been really difficult to get the…
MattB
  • 2,053
  • 5
  • 21
  • 44
11
votes
2 answers

Excel - VBA : pass variable from Sub to Userform

I have read and applied solution I found on similar topics but nothing seem to work in my case. So, I want to pass a variable from one sub of my Module1 to a userform. It's a string called "provinceSugg". Here is the relevant part of my code :…
Phalanx
  • 1,187
  • 5
  • 24
  • 35
10
votes
5 answers

Assign on-click VBA function to a dynamically created button on Excel Userform

I'm creating buttons dynamically on an Excel userform with the following code: With Me.CurrentFrame.Controls.Add("Forms.CommandButton.1") .Caption = "XYZ" .name = "AButton" .Font.Bold = True .ForeColor = &HFF& ... blah blah…
notnot
  • 4,163
  • 11
  • 41
  • 54
10
votes
5 answers

Why does Showing a UserForm as Modal Stop Code Execution?

The following VBA code stops at Me.Show. From my tests, it seems that Me.Show stops all code execution, even if the code is inside the UserForm. This part is outside the UserForm: Public Sub TestProgress() Dim objProgress As New UserForm1 …
Kuyenda
  • 4,119
  • 10
  • 43
  • 62
10
votes
2 answers

Excel VBA Open workbook, perform actions, save as, close

This question has been edited due to lengthy comments and updates from proposed answers. As requested here is module 13; Sub SaveInFormat() Application.DisplayAlerts = False Workbooks.Application.ActiveWorkbook.SaveAs Filename:="C:\Documents and…
JamesDev
  • 223
  • 2
  • 4
  • 15
10
votes
2 answers

vba userforms carriage return behavior

In a textbox of a userform in VBA, I would like to enable the feature where the user can add a new line in the textbox by pressing the "Enter" key on the keyboard. However, I had trouble finding a UI option in the UI editor to achieve this. Is this…
stanigator
  • 10,102
  • 32
  • 88
  • 126
9
votes
0 answers

Why can't I `End` code while I'm subclassing without breaking everything?

I've written some code in VBA to subclass a userform so that ultimately I can intercept WM_TIMER messages being dispatched to it. I'm doing this instead of specifying a TIMERPROC, as it allows me to use VBAs own error handling and calling methods to…
Greedo
  • 3,465
  • 2
  • 19
  • 49
9
votes
11 answers

Set focus back to the application window after showing userform

When showing a userform (running its Show method) it not only shows up on the screen but also takes the focus (the destination of e.g. keystrokes). Say, the userform is a custom made toolbar. Its Show fires in Workbook_Open() but the form itself is…
Greenberet
  • 440
  • 1
  • 7
  • 16
1
2 3
99 100