Userform write to multiple textboxes on specific pages

WildBurrow

New Member
Joined
Apr 5, 2021
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I found the following code that provides the function I need (sorry, I forgot to snag contributor's name).

VBA Code:
Private Sub txtPriCO1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyDelete, _
        vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
            If KeyAscii = 46 Then If InStr(1, txtPriCO1.Text, ".") Then KeyAscii = 0
        Case Else
            KeyAscii = 0
            Beep
    End Select
End Sub

I'd like to group this code to accommodate multiple text boxes on specific pages on the Userform. While I understand that I can address a specific page with this: "For Each Ctrl In Me.MultiPage1.Pages(2).Controls)", I don't know how to write the code for KeyPress.

I have 25 such text boxes on pages 3, 4, and 5 of the Userform (I know the pages collection is zero-based).
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I should add that there are other textboxes on these same pages that I do not want to include in the above code. Therefore, I'm thinking I need to create an array?
 
Upvote 0
I should add that there are other textboxes on these same pages that I do not want to include in the above code. Therefore, I'm thinking I need to create an array?
Final entry....

I found a solution here: How can i make a sub for a keypress event?. Thanks to Siddarth Rout!!

I created a Class Module as follows:

VBA Code:
Option Explicit

Public WithEvents TextBoxEvents As MSForms.TextBox

Private Sub TextBoxEvents_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyDelete, _
        vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
            If KeyAscii = 46 Then If InStr(1, TextBoxEvents, ".") Then KeyAscii = 0
        Case Else
            KeyAscii = 0
            Beep
        End Select
    End Sub


Then placed the following code in the forms module:

Option Explicit
Dim myTBs() As New Class1
'Format Numberic Textboxes
Private Sub UserForm_Initialize()
Dim i As Integer, objControl As Control

For Each objControl In Me.Controls
If TypeOf objControl Is MSForms.TextBox Then
Select Case objControl.Name
'Methane
Case "txtPriMeth1", "txtPriMeth2", "txtPriMeth3", "txtPriMeth4", "txtPriMeth5", _
"txtPriMeth6", "txtPriMeth7", "txtPriMeth8", "txtPriMeth9", "txtPriMeth10", _
"txtSecMeth1", "txtSecMeth2", "txtSecMeth3", "txtSecMeth4", "txtSecMeth5", _
"txtPriCO1", "txtPriCO2", "txtPriCO3", "txtPriCO4", "txtPriCO5", _
"txtPriCO6", "txtPriCO7", "txtPriCO8", "txtPriCO9", "txtPriCO10", _
"txtSecCO1", "txtSecCO2", "txtSecCO3", "txtSecCO4", "txtSecCO5", _
"txtPriH2S1", "txtPriH2S2", "txtPriH2S3", "txtPriH2S4", "txtPriH2S5", _
"txtPriH2S6", "txtPriH2S7", "txtPriH2S8", "txtPriH2S9", "txtPriH2S10", _
"txtSecH2S1", "txtSecH2S2", "txtSecH2S3", "txtSecH2S4", "txtSecH2S5", _
"txtPriVel1", "txtPriVel2", "txtPriVel3", "txtPriVel4", "txtPriVel5", _
"txtPriVel6", "txtPriVel7", "txtPriVel8", "txtPriVel9", "txtPriVel10", _
"txtSecVel1", "txtSecVel2", "txtSecVel3", "txtSecVel4", "txtSecVel5", _
"txtPriTemp1", "txtPriTemp2", "txtPriTemp3", "txtPriTemp4", "txtPriTemp5", _
"txtPriTemp6", "txtPriTemp7", "txtPriTemp8", "txtPriTemp9", "txtPriTemp10", _
"txtSecTemp1", "txtSecTemp2", "txtSecTemp3", "txtSecTemp4", "txtSecTemp5", _
"txtAmbTemperature", "txtPrecipitation", "txtWindSpeed", "txtBarometric"
i = i + 1
ReDim Preserve myTBs(1 To i)
Set myTBs(i).TextBoxEvents = objControl
End Select
End If
Next objControl
Set objControl = Nothing
VBA Code:
End Sub


I did have some confusion at first as I had initially placed the above code UNDER other lines of code. It didn't like that. Once I put this code at the very top of the page and then put the other form initialization code under it, then everything worked great.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top