Add decimal point at specific position

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to loop through a series of Textboxes on a Userform and enter a decimal point after the first 2 numbers entered;

Code:
Dim ctrl As MSForms.controlFor Each ctrl In Controls
If ctrl.Name Like "TextSTime*" Then
If Len(ctrl.Value) = 3 Then ctrl.Value = Left(ctrl.Value, 2) & "." & Right(ctrl.Value, 1)
End If
Next

I need something similar now - what I need is for it to add the decimal point before the last 2 numbers, regardless of how many are entered, (although I have limited the Textboxes to a maximum of 6 characters).

Some examples of what I'd expect to see:


Anyone?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,

How about Left(ctrl.Value, Len(ctrl.Value)-2) & "." & Right(ctrl.Value, 2)

EDIT: Length must be at least 2
 
Last edited:
Upvote 0
How about
Code:
Dim ctrl As MSForms.Control
For Each ctrl In Controls
   If ctrl.Name Like "TextSTime*" Then
      ctrl.Value = ctrl.Value / 100
   End If
Next
 
Upvote 0
Guys,

I've tried this:

Code:
Dim ctrl As MSForms.controlFor Each ctrl In Controls
If ctrl.Name Like "TextSTime*" Then
If Len(ctrl.Value) = 3 Then ctrl.Value = Left(ctrl.Value, Len(ctrl.Value) - 2) & "." & Right(ctrl.Value, 2)
End If
Next

but if I enter "9825" that gives me:

I've also tried this:

Code:
Dim ctrl As MSForms.controlFor Each ctrl In Controls
   If ctrl.Name Like "TextSTime*" Then
      ctrl.Value = ctrl.Value / 100
   End If
Next

but that gives me 'Type Mismatch' on this line:

Code:
ctrl.Value = ctrl.Value / 100

Just to confirm, these are textboxes on a userform, not a worksheet - any ideas?
 
Upvote 0
Are all those textboxes numbers only?
If so & some can be empty try
Code:
      If ctrl <> "" Then ctrl.Value = ctrl.Value / 100
 
Upvote 0
Cheers Fluff - yes, they are....your suggestion works, but only if I put it in the exit event - I was hoping it could be done on the change event so it's formatted as they type, can that be done? No worries if not.
 
Upvote 0
Why on earth are you looping through all the textboxes with a change event?

Also as you are changing the value in the textbox, it's best not to run that in a change event.
Try something like
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   With Me.TextBox1
      If Len(.Value) > 0 Then .Value = .Value / 100
   End With
End Sub
 
Upvote 0
I'm doing it in the change event because I wanted it to format as they type instead of having to wait for them to tab out for it to be formatted.....
 
Last edited:
Upvote 0
Do you have any code in the change event to prevent it from triggering itself?
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

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