PDA

View Full Version : Help With Applying Numeral Limitations in VBA Forms



harber95
07-20-2015, 04:02 PM
I have a user form in which the user is supposed to insert numbers into two text boxes. It's important that the numbers will follow the following conditions:

1. It must be a number (obviously).
2. The number must be natural (positive and whole).
3. It mustn't be larger than a certain value (in my case, it can't be above 1500. I would like that the code given to me will have that number as an example)
4. One of the lines needs to be a matrix. The maximum limit is 250x250. Maybe there is a special way to insert a matrix that I'm not aware of.

If the conditions are not met, then a message box must appear as a response.

Thanks so much for the help!

mikerickson
07-20-2015, 11:40 PM
This code will take care of conditions 1-3.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim newString As String

Rem restrict non-negative integer entry only
If Chr(KeyAscii) Like "[!0-9]" Then Beep: KeyAscii = 0

Rem impose maximum value
With TextBox1
newString = Left(.Text, .SelStart) & Chr(KeyAscii) & Mid(.Text, .SelStart + .SelLength + 1)
End With

If 1500 < Val(newString) Then
MsgBox "maximum value is 1500"
KeyAscii = 0
End If
End Sub
I don't understand condition 4. "One of the lines needs to be a matrix"
All user entries into a text box are strings, what strings might be entered as a matri

harber95
07-21-2015, 01:18 AM
First, I thought that I don't need to use a string, because I thought that it usually applies to text not numbers.
As for the matrix, I don't know how to describe it, but the matrix is the area that we choose to work with in the worksheet. Within the chosen area, we assign a number of marked cells that are supposed to move in the selected area. And right now, I'm working on the userform where I'm supposed to enter the size of the matrix.
Anyways, thanks for help.

mikerickson
07-21-2015, 06:41 AM
It sounds like you are using "matrix" to describe a Range.

You can use a RefEdit control to allow the user to select a range with the mouse.

harber95
07-21-2015, 09:50 AM
I'm a newbie. What's RefEdit?

mikerickson
07-21-2015, 11:08 AM
Its one of the controls that can be put in a userform.

harber95
07-21-2015, 11:49 AM
It sounds like you are using "matrix" to describe a Range.

You can use a RefEdit control to allow the user to select a range with the mouse.

the range that I choose must be done with the user forms alone.

Bob Phillips
07-21-2015, 12:07 PM
RefEdit would be a control on the form, it's like a special textbox. When you click it and the select a range, that range address will show in the RefEdit.

harber95
07-21-2015, 12:40 PM
I saw a tutorial on refedit, and I think that I need to find another way to define the range by filling in a textbox with the Height X Width (Height = Width). Please tell me if you know another way.

mikerickson
07-23-2015, 11:19 AM
You should stick with range addresses, everyone knows them.
I don't know why a RefEdit would be a problem, but a Textbox where the user types the range address would be another option. Perhaps paired with another textbox to indicate the sheet.

Bob Phillips
08-01-2015, 08:08 AM
RefEdit is the way to go. The user then uses the mouse to select the range (width and height), and the address gets placed in the RefEdit box.