Oay, let's imagine for the sake of this exercise that the first
If TextBox167.Text = " GRAND TOTAL" Then and the
Call SheetToBox("G21", 165) statements aren't included in this discussion.
Option 1: move all of that code into a separate module, wrap it in a pair of
Public Sub DoBox165() and
End Sub lines, and then where you removed the code put in
Call DoBox165. Repeat for other boxes as required.
Option 2 (neater, in my opinion): if the processing is the same for all those text boxes, you could make the code more general purpose, like so:-
Code:
[FONT=Fixedsys]Sub SetTextBox(ByVal [COLOR=red]aBox[/COLOR] As Integer)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text <> "-" Then
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text <> "" Then
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text <= 1 Then UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).BackColor = RGB(0, 255, 0)
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text <= 1 Then UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).ForeColor = RGB(0, 0, 0)
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text > 1 Then UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).BackColor = RGB(255, 0, 0)
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text > 1 Then UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).ForeColor = RGB(0, 0, 0)
End If
End If
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text = "-" Or UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text = "" Then UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).BackColor = RGB(128, 128, 128)
If UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text = "-" Or UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).Text = "" Then UserForm1.Controls("TextBox" & [COLOR=red]aBox[/COLOR]).ForeColor = RGB(0, 0, 0)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]End Sub
[/FONT]
Then in place of the original code you would do
Call SetTextBox(165). To process
TextBox165, etc. You can name the actual
Sub whatever you like as long as you
Call it by its correct name. I've highlighted the code where the text box number is passed in and used just for the purpose of clarity (hopefully).
In fact that code can be made slightly neater (I think) by using a
With..End With block:-
Code:
[FONT=Fixedsys]Sub SetTextBox(ByVal aBox As Integer)[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] With UserForm1.Controls("TextBox" & aBox)
If [COLOR=red].[/COLOR]Text <> "-" Then
If [COLOR=red].[/COLOR]Text <> "" Then
If [COLOR=red].[/COLOR]Text <= 1 Then [COLOR=red].[/COLOR]BackColor = RGB(0, 255, 0)
If .Text <= 1 Then [COLOR=red].[/COLOR]ForeColor = RGB(0, 0, 0)
If [COLOR=red].[/COLOR]Text > 1 Then [COLOR=red].[/COLOR]BackColor = RGB(255, 0, 0)
If [COLOR=red].[/COLOR]Text > 1 Then [COLOR=red].[/COLOR]ForeColor = RGB(0, 0, 0)
End If
End If
If [COLOR=red].[/COLOR]Text = "-" Or [COLOR=red].[/COLOR]Text = "" Then [COLOR=red].[/COLOR]BackColor = RGB(128, 128, 128)
If [COLOR=red].[/COLOR]Text = "-" Or [COLOR=red].[/COLOR]Text = "" Then [COLOR=red].[/COLOR]ForeColor = RGB(0, 0, 0)
End With[/FONT]
[FONT=Fixedsys]End Sub[/FONT]
Again, I've highlighted in red the dots which mean "use the object specified in the
With clause to reference this property".
Once again I'd recommend you make these changes to a
copy of your workbook - in fact make uniquely named 'security copies' ever 10-15 minutes or so, so that you can roll back to a known working version when you make a mistake which stops everything functioning correctly. This happens to everyone sooner or later!