Number formats in Textboxes being removed when printing

KenC53

New Member
Joined
Jul 24, 2011
Messages
14
I have the following code to conditionally format two text boxes based on their cell values and to format the numbers in the Text Boxes in a specific format. When I go to print the sheet the conditional formatting in the text boxes remains but the number formatting that is set by the VBA is removed. Text box 1 prints as something like 102.35241526359854 and Text Box 2 prints like .145368549521. Any ideas why this is happening?

Private Sub TextBox1_Change()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
If TextBox1.Text < 45 Then TextBox1.BackColor = RGB(165, 194, 106)<o:p></o:p>
If TextBox1.Text < 45 Then TextBox1.ForeColor = RGB(0, 0, 0)<o:p></o:p>
If TextBox1.Text >= 45 And TextBox1.Text < 120 Then TextBox1.BackColor = RGB(255, 255, 50)<o:p></o:p>
If TextBox1.Text >= 45 And TextBox1.Text < 120 Then TextBox1.ForeColor = RGB(0, 0, 0)<o:p></o:p>
If TextBox1.Text >= 120 Then TextBox1.BackColor = RGB(245, 70, 70)<o:p></o:p>
If TextBox1.Text >= 120 Then TextBox1.ForeColor = RGB(0, 0, 0)<o:p></o:p>
If TextBox1.Text = "-" Or TextBox1.Text = "" Then TextBox1.BackColor = RGB(128, 128, 128)<o:p></o:p>
If TextBox1.Text = "-" Or TextBox1.Text = "" Then TextBox1.ForeColor = RGB(0, 0, 0)<o:p></o:p>
<o:p> </o:p>
TextBox1.Text = Format(val(TextBox1.Text), "0")<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Private Sub TextBox2_Change()<o:p></o:p>
<o:p> </o:p>
Dim sTemp As String, val As Single, val2 As Single<o:p></o:p>
sTemp = TextBox2.Text<o:p></o:p>
If Not (InStr(1, sTemp, "%", vbTextCompare) < 1 _<o:p></o:p>
And IsNumeric(sTemp)) Then _<o:p></o:p>
Exit Sub '<<<<<<o:p></o:p>
<o:p> </o:p>
val = CSng(sTemp)<o:p></o:p>
val2 = IIf(val < 1, val, val / 100)<o:p></o:p>
TextBox2.Text = Format(val2, "0%")<o:p></o:p>
<o:p> </o:p>
If val2 < 0.04 Then TextBox2.BackColor = RGB(165, 194, 106)<o:p></o:p>
If val2 < 0.04 Then TextBox2.ForeColor = RGB(0, 0, 0)<o:p></o:p>
If val2 >= 0.04 And val2 < 0.12 Then TextBox2.BackColor = RGB(255, 255, 50)<o:p></o:p>
If val2 >= 0.04 And val2 < 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)<o:p></o:p>
If val2 >= 0.12 Then TextBox2.BackColor = RGB(245, 70, 70)<o:p></o:p>
If val2 >= 0.12 Then TextBox2.ForeColor = RGB(0, 0, 0)<o:p></o:p>
If TextBox2.Text = "-" Or TextBox2.Text = "" Then TextBox2.BackColor = RGB(128, 128, 128)<o:p></o:p>
If TextBox2.Text = "-" Or TextBox2.Text = "" Then TextBox2.ForeColor = RGB(0, 0, 0)<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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