VBA Excel UserForm Simple Question

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Excel
Click For Summary

Discussion Overview

The discussion revolves around programming a UserForm in VBA for Excel, specifically focusing on how to efficiently store values from multiple TextBoxes into an array. Participants explore various coding techniques, address issues encountered, and share examples of their approaches.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks a method to store values from TextBoxes named "Powerbox1", "Powerbox2", etc., into an array without assigning each value individually.
  • Another participant provides an example using a loop to store values from three TextBoxes into a 2D array, noting the need to avoid including command button values.
  • A participant expresses surprise at the inability to directly reference TextBox names for different parameters, suggesting a naming convention for easier management.
  • Discussion includes the limitation of Excel's lack of support for certain commands available in Access, which complicates the approach.
  • One participant proposes using separate forms for different parameters to manage values more effectively.
  • Another participant shares a modified code example that results in a runtime error, prompting questions about type mismatches and array dimensions.
  • A participant discusses the challenge of iterating through controls in a UserForm, noting issues with the number of controls exceeding the array size and considering the use of ReDim to adjust the array size.
  • Participants note that dimensioning arrays differently can affect outcomes, leading to further questions about the underlying reasons for these behaviors.

Areas of Agreement / Disagreement

Participants express various methods and experiences, but there is no consensus on a single solution. Multiple competing views and unresolved issues remain regarding the best approach to handle the UserForm controls and array storage.

Contextual Notes

Participants mention limitations related to Excel's functionality compared to Access, as well as issues with runtime errors and type mismatches that arise from different coding approaches.

Who May Find This Useful

Individuals interested in VBA programming for Excel, particularly those working with UserForms and seeking to manage multiple input fields efficiently.

Saladsamurai
Messages
3,009
Reaction score
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
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
 
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)
 
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
 
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.
 
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?
 
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.
 
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
 
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?
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
5
Views
4K
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 6 ·
Replies
6
Views
5K