Consulting

Results 1 to 11 of 11

Thread: Help With Applying Numeral Limitations in VBA Forms

  1. #1

    Unhappy Help With Applying Numeral Limitations in VBA Forms

    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!

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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

  3. #3
    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.

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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.

  5. #5
    I'm a newbie. What's RefEdit?

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Its one of the controls that can be put in a userform.

  7. #7
    Quote Originally Posted by mikerickson View Post
    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.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    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.

  10. #10
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •