Simplifying the code for formatting all textboxes after update.

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Is there a way to simplify this coding?

VBA Code:
Private Sub txtAmount1_AfterUpdate()
    Dim txt As Object
    Set txt = Me.txtAmount1
        
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
Private Sub txtAmount2_AfterUpdate()
    Dim txt As Object
    Set txt = Me.txtAmount2
        
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
Private Sub txtAmount3_AfterUpdate()
    Dim txt As Object
    Set txt = Me.txtAmount3
        
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
Private Sub txtAmount4_AfterUpdate()
    Dim txt As Object
    Set txt = Me.txtAmount4
        
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
Private Sub txtAmount5_AfterUpdate()
    Dim txt As Object
    Set txt = Me.txtAmount5
        
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I havent tested this but give it a try


VBA Code:
Private Sub txtAmount1_AfterUpdate()
    FormatText Me.txtAmount1
End Sub
Private Sub txtAmount2_AfterUpdate()
    FormatText Me.txtAmount2
End Sub
Private Sub txtAmount3_AfterUpdate()
    FormatText Me.txtAmount3
End Sub
Private Sub txtAmount4_AfterUpdate()
    FormatText Me.txtAmount4
End Sub
Private Sub txtAmount5_AfterUpdate()
    FormatText Me.txtAmount5
End Sub

Private Sub FormatText(ByVal txt as Object)
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
 
Upvote 0
Solution
I havent tested this but give it a try


VBA Code:
Private Sub txtAmount1_AfterUpdate()
    FormatText Me.txtAmount1
End Sub
Private Sub txtAmount2_AfterUpdate()
    FormatText Me.txtAmount2
End Sub
Private Sub txtAmount3_AfterUpdate()
    FormatText Me.txtAmount3
End Sub
Private Sub txtAmount4_AfterUpdate()
    FormatText Me.txtAmount4
End Sub
Private Sub txtAmount5_AfterUpdate()
    FormatText Me.txtAmount5
End Sub

Private Sub FormatText(ByVal txt as Object)
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
        If txt.Value = "" Then txt.Value = "0.00"
    End With
End Sub
Beautiful! Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,215,402
Messages
6,124,708
Members
449,182
Latest member
mrlanc20

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