Apply the same formatting code to multiple TextBoxes

JABJC

New Member
Joined
Aug 3, 2015
Messages
10
I know very little about VBA code, but I managed to copy and paste code(s) from various site to create a userform. I have multiple TextBoxes (i.e., TextBox1 thru TextBox15) on the userform. I would like 10 of the 15 text boxes formatted with the same code (see below). I copied the code 10 times, then changed the name (e.g., TextBox1, TextBox2 ....TextBox10) so the code is just repeats.

Is it possible to reduce the amount of code and apply the code below it to the 10 TextBoxes that uses the same format with having to repeat the code?

Private Sub TextBox1_Change()
With TextBox1
If .Value < 0 Then
.BackStyle = fmBackStyleTransparent
.ForeColor = fmBackStyleTransparent
.ForeColor = RGB(255, 0, 0)
Else
.ForeColor = fmBackStyleTransparent
.ForeColor = RGB(0, 0, 0)
End If
End With
End Sub

Thank you
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Create a Class module and rename it to ClTxtBx then paste this code into it
Code:
[COLOR=#0000ff]Public WithEvents TextBxGroup As MSForms.TextBox[/COLOR]

Private Sub TextBxGroup_Change()
   With TextBxGroup
      If .Value < 0 Then
         .BackStyle = fmBackStyleTransparent
         .ForeColor = fmBackStyleTransparent
         .ForeColor = RGB(255, 0, 0)
      Else
         .ForeColor = fmBackStyleTransparent
         .ForeColor = RGB(0, 0, 0)
      End If
   End With
End Sub
Then in the userform module
Code:
[COLOR=#0000ff]Dim TxtBx() As New ClTxtBx[/COLOR]

Private Sub UserForm_Initialize()
   Dim i As Long
   Dim ctrl As MSForms.Control
   
   ReDim TxtBx(1 To 10)
   For i = 1 To 10
      Set TxtBx(i).TextBxGroup = Me.Controls("Textbox" & i)
   Next i
End Sub
The lines in blue must go at the very top of their respective modules, before any code
 
Upvote 0

Forum statistics

Threads
1,215,729
Messages
6,126,524
Members
449,316
Latest member
sravya

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