Userform textbox %


Posted by Dave on December 16, 2001 8:35 PM

Could someone please help me. why is this code not working? Cell A8 if formated percentage - Cell B8 contains a formula Any Help would be appreciated. I'm just trying to be able to enter a whole number in a textbox and its value looking the same in the textbox and cell!

Private Sub UserForm_Initialize()
showupdates
End Sub

Private Sub showupdates()
Label1.Caption = Format(ActiveWorkbook.Sheets_("a").Range("B8").Value,_
"#,##0")
TextBox1.Value = Format(Worksheets("a").Range_("A8").Value, "0.00%")
End Sub

Private Sub textbox1_AfterUpdate()
Worksheets("a").Range("A8").Value =_ ((TextBox1.Value) / 100)
showupdates
End Sub

TIA

Posted by Damon Ostrander on December 16, 2001 11:24 PM

Hi Dave,

It looks to me like the reason it is not working is that the textbox1 AfterUpdate event loads a value into A8 and calls showupdates. Then showupdates formats the value of A8 and puts it into textbox1. This then triggers the textbox1 AfterUpdate event again, and the cycle continues indefinitely. Is this the problem you are seeing? If so, just eliminate the call to showupdates in the AfterUpdate event. If you do need to update the label, just put that code directly into the AfterUpdate event, but don't update the textbox1 value.

Damon

Posted by Dave on December 17, 2001 6:14 AM

Damon,

Thanks for the answer. But I do need the label updated and the textbox updated. What is happening here is the textboxt is a percentage value the label shows a value (which is a formula that includes the textboxt value). I want to be able to show the textbox (cell) value as say 6% and if needed to change that value type 7 into the textbox and it will show as 7% in the textbox and cell and will be used in the formula as .07.

Posted by Damon Ostrander on December 17, 2001 8:50 AM

Hi again Dave,

Okay, I think I understand now. But the problem remains that it is not only manual entry into the textbox that triggers the AfterUpdate event--your showupdates procedure also triggers it, and this should cause an infinite loop. I believe that for what you want to do you should turn event handling off in the showupdates routine so it doesn't itself trigger the AfterUpdates event:

Private Sub showupdates()
Application.EnableEvents = False
Label1.Caption = Format(ActiveWorkbook.Sheets_("a").Range("B8").Value,_
"#,##0")
TextBox1.Value = Format(Worksheets("a").Range_("A8").Value, "0.00%")
Application.EnableEvents = True
End Sub

Give it a try.

Damon ,

Posted by Juan Pablo G. on December 17, 2001 12:57 PM

Also, give it a try using the BeforeUpdate event instead of the AfterUpdate event.

Juan Pablo G. Hi again Dave, Okay, I think I understand now. But the problem remains that it is not only manual entry into the textbox that triggers the AfterUpdate event--your showupdates procedure also triggers it, and this should cause an infinite loop. I believe that for what you want to do you should turn event handling off in the showupdates routine so it doesn't itself trigger the AfterUpdates event:

Posted by Dave on December 17, 2001 2:14 PM

Hey juan,

Thanks for the help but thaty does not work the code gets stuck at this line

Worksheets("a").Range("A8").Value =_ ((TextBox1.Value) / 100)

You see I'm trying to enter say the nmumber 9 in the textbox have it look like 9% in the textbox and update the label and have the cell look like 9%!! I've asked at least 20 people on how do do this in various VBA places I've not been given an explanation or answer YET!

Also, give it a try using the BeforeUpdate event instead of the AfterUpdate event. Juan Pablo G. : Hi again Dave, : Okay, I think I understand now. But the problem remains that it is not only manual entry into the textbox that triggers the AfterUpdate event--your showupdates procedure also triggers it, and this should cause an infinite loop. I believe that for what you want to do you should turn event handling off in the showupdates routine so it doesn't itself trigger the AfterUpdates event:




Posted by Juan Pablo G. on December 17, 2001 7:48 PM

Ok, here's what i do (In fact, doing at this same moment). I have one label and one textbox, to make it simple, and one command button (All with default names).

I work with range A1 on Sheet1 to get the data of TextBox1.

I have this data on the UserForm module, i hope it does what you want.

Option Explicit

Private Sub CommandButton1_Click()
If TextBox1 <> "" Then 'Copy it to sheet as number (Not %)
With Range("A1")
.Value = Left(TextBox1, Len(TextBox1) - 1) / 100
.NumberFormat = "0.00%"
End With
Unload Me
Else 'No number in TextBox1
MsgBox "Sorry, no number yet"
End If
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox1) Then 'Is real number ? like 8.45 ?
TextBox1 = Format(TextBox1 / 100, "0.00%")
Label1 = TextBox1
ElseIf Right(TextBox1, 1) = "%" Then 'Is string like 5.67% ?
Label1 = TextBox1
Else 'Delete contents, not a number
TextBox1 = ""
Label1 = "Please enter a number"
End If
End Sub

Private Sub UserForm_Initialize() 'Get actual number from A1
Label1 = Format(Range("A1"), "0.00%")
TextBox1 = Format(Range("A1"), "0.00%")
End Sub

Juan Pablo G.