Achieve specific behavior in a textbox (Crosspost)

kapela2017

New Member
Joined
Oct 16, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
In accordance with what is indicated in Rule # 13 of the forum, I will limit the following
Link

Post details

Greetings seniors, I've been trying for a while to get a text box to behave in a specific way, which I'll try to briefly describe, I need the values to be entered from right to left, like on a calculator and the format should have "Kg" concatenated either It must be necessary to enter the comma because it is automatically positioned, I attach the Work Code in the Two textboxes can be seen But this must be done in a single textbox without the need to have another text box as an auxiliary, I get the impression that this will be easier with a Udf, but I remain attentive to your suggestions

VBA Code:
Option Explicit

Dim PreviousValue As String
Dim ProgramChange As Boolean

Private Sub TextBoxDataEntry_Change()

   Dim FormattedNumber As String
   
   If ProgramChange Then Exit Sub
   
   If TextBoxDataEntry = "" Then
      TextBoxDataDisplay = ""
   ElseIf Not IsNumeric(TextBoxDataEntry) Then
      MsgBox "Entry must be numeric"
      ProgramChange = True
      TextBoxDataEntry = PreviousValue
      ProgramChange = False
   ElseIf CLng(TextBoxDataEntry) > 9999 Then
      MsgBox "Entry must be numeric 0 - 9999"
      ProgramChange = True
      TextBoxDataEntry = PreviousValue
      ProgramChange = False
   Else
      FormattedNumber = Format(TextBoxDataEntry, "0000")
      TextBoxDataDisplay = Left(FormattedNumber, 2) & "," & Right(FormattedNumber, 2) & " Kg"
      PreviousValue = TextBoxDataEntry
   End If

End Sub
 

Attachments

  • Input in textbox from right to left with comma positioning.png
    Input in textbox from right to left with comma positioning.png
    29.7 KB · Views: 11
OK. As for making it dynamic, I'm not up to the challenge. In that case, I think I agree with those who said that trying to do that in one control is overly complex (my words). The dynamics of how event procedures interact (e.g. Change, AfterUpdate, BeforeUpdate, key events) just present too much of a challenge, especially when there's no remuneration. Anyway, it was almost fun!
Good luck.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

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