VBA Excel UserForm Simple Question

  • Thread starter Saladsamurai
  • Start date
  • Tags
    Excel
In summary, the code is trying to store the values of the userform's textboxes in an array, but is getting an error because the number of textboxes is not consistent with the number of controls in the userform.
  • #1
Saladsamurai
3,020
7
:smile: Hello there!

I have created a Useform with a few rows of TextBoxes.

I have Changed the names of them all to "Powerbox1" , "Powerbox2" ...etc.


Now in my code, I would like to store all of those values taken from the UserForm and store them in an array called Power()

I have been trying things like

Code:
Private Sub OkayButton_Click()

    Dim Power(48) As Single
    Dim i As Integer
    
    
    For i = 1 To 48
        
        Power(i) = PowerForm.Powerbox(i).Text
    Next i
    
    MsgBox Power(1)
    
    
    
    


End Sub

But this method does not work.

Is there anyway to assign values to this array without doing them individually?
 
Technology news on Phys.org
  • #2
Hello,

Here is an example. I just used three text boxes. You have to make sure it doesn't pick up the value for the command button.

Code:
Private Sub CommandButton1_Click()

Dim i As Integer
Dim test(3, 0) As Integer

For i = 0 To 3
    test(i, 0) = UserForm1.Controls(i)
    MsgBox test(i, 0)
Next
End Sub

If you enter an integer in each text box the message box will display each value as you iterate through the loop. You will also get a value of zero on the last iteration of the loop. I believe that is the value of the command button.

Hope this helps.

Thanks
Matt
 
  • #3
I will try this when I get home. Thanks!

What surprises me, though, is that you can't affix the particular
name of the text boxes.

For example: what if the form has boxes for 'power' & 'flowrate'
and I want to store all power values in one array and all flows
in another?

It would seem that I could just name the textboxes
'powerbox1 ...' and 'flowbox1...'

and then use something like

PowerArray(i)=Userform.Powerbox(i)
 
  • #4
In Excel there is no support for the .ControlsType command. This command is supported in Access. I use it frequently in Access to loop through a form or subform that contains a group of checkboxes and to ask the user to enter information depending on what checkbox is selected.

Your thinking is logical but Excel isn't.

Thanks
Matt
 
  • #5
So I guess the solution to this would be to use separate
forms for each parameter and then pass the values
between the different codes.
 
  • #6
CFDFEAGURU said:
Hello,

Here is an example. I just used three text boxes. You have to make sure it doesn't pick up the value for the command button.

Code:
Private Sub CommandButton1_Click()

Dim i As Integer
Dim test(3, 0) As Integer

For i = 0 To 3
    test(i, 0) = UserForm1.Controls(i)
    MsgBox test(i, 0)
Next
End Sub

Hmmm... I seem to be having some issues with this code.

I simply modified it to be a 1-D Array. And I used Option Base 1 (i.e. the first element of array is index=1 as opposed to zero).

Code:
Option Explicit
Option Base 1

Private Sub OKButton_Click()
    Dim i As Integer
    Dim Test(3) As Integer
    
    For i = 1 To 3
        Test(i) = PowerForm.Controls(i)
        MsgBox Test(i)
    Next i

End Sub

I also have a sub that is simply :

Code:
Sub Test ()
PowerForm.Show
End Sub

When I run the latter, my UserForm "PowerForm" pops up. I enter 1, 2, 3 into the first 3 Textboxes TextBox1 , TextBox2 , TextBox3 , respectively.

The Message boxes start to pop up: It does not show "1"...only "2" "3" and then a friggin' "Runtime Error '13' : Type MisMatch" shows up.


I am not sure what to make of this? Any ideas?
 
  • #7
Okay this almost works:

Code:
Option Explicit
Option Base 1

Private Sub OKButton_Click()
    
    Dim i As Integer
    Dim Test(12) As Variant
    Dim TextBox As Control
    
    i = 1
    For Each TextBox In Me.Controls
        Test(i) = TextBox
        MsgBox Test(i)
        i = i + 1
        
    Next TextBox

End Sub

The problem is this: My Test() array is only dimmed to 12 (i.e. the # of textboxes). However, the # of controls is 14----> 12 TextBoxes + OK_Button + Cancel_Button

So I am getting a subscript out of Range error because it is a stupid For Loop.

Stupid in the sense that is ambiguous "For Each .." does not allow me to set the bounds of the For Loop.

I guess I could just Cut Off the 2 extra terms and ReDim the array.
 
  • #8
The Message boxes start to pop up: It does not show "1"...only "2" "3" and then a friggin' "Runtime Error '13' : Type MisMatch" shows up.

Yes, I got the same until I dimensioned the array as (3,0).

I guess I could just Cut Off the 2 extra terms and ReDim the array.

That is exactly what I was going to do.

Thanks
Matt
 
  • #9
CFDFEAGURU said:
Yes, I got the same until I dimensioned the array as (3,0).



Thanks
Matt

hmmm... I wonder why redimming it as 2D would fix that?
 

1. What is a VBA Excel UserForm?

A VBA Excel UserForm is a graphical user interface tool used in Microsoft Excel to create custom dialog boxes and forms for data entry and display. It allows users to interact with the spreadsheet and perform actions such as data input, data editing, and data analysis.

2. How do I create a UserForm in VBA Excel?

To create a UserForm in VBA Excel, go to the Developer tab and click on the "Insert" button. Then, select "UserForm" from the drop-down menu. This will open a blank UserForm, which you can customize by adding controls and coding functionalities using VBA programming language.

3. What are controls in VBA Excel UserForms?

Controls in VBA Excel UserForms are interactive elements, such as buttons, text boxes, drop-down lists, etc., that allow users to input or display data. These controls can be added to the UserForm by using the "Toolbox" menu in the Developer tab.

4. How do I code a UserForm in VBA Excel?

To code a UserForm in VBA Excel, you can use the Visual Basic Editor (VBE), which can be accessed by pressing "Alt + F11" or by going to the Developer tab and clicking on the "Visual Basic" button. In the VBE, you can write VBA code to add functionality to the controls on the UserForm.

5. Can I add UserForms to my Excel workbook?

Yes, you can add UserForms to your Excel workbook by going to the Developer tab and clicking on the "Insert" button. Then, select "UserForm" from the drop-down menu. This will add a blank UserForm to your workbook, which you can customize and code as needed.

Similar threads

  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Other Physics Topics
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
18
Views
5K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
Back
Top