Reducing VBA redundancy

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,075
Office Version
  1. 2019
Platform
  1. Windows
I have numerous textboxes that use the code below in their respective change event. In the effort to reduce redundancy, is it possible to have just one code and call the code in each textbox change event?

VBA Code:
Select Case Len(tbEq)
    Case 1
        tbEq = Format(tbEq, "$0\.00")
    Case Is > 1
        v = Replace(tbEq, "$", "")
        v = Replace(v, ".", "")
        tbEq = Format(v / 100, "$#,#0.00")
    Case Else
End Select
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You could create Subroutine and have the On_Change event call it?
 
Upvote 0
I'm not a savvy coder and the code I posted I found using google. Can someone assist with writing a sub-routine that I can use?

Thanks
 
Upvote 0
All of the TextBoxes have that same code in them?
 
Upvote 0
Try this.
VBA Code:
Sub test()

thisButton = Application.Caller
Call buttonCode

End Sub
Sub buttonCode()

' Your TextBox code here

End Sub
 
Upvote 0
Nothing happens, but I suspect its because each textbox has a different name. The example code I posted, the textbox name is tbEq.
 
Upvote 0
Hi
Try following & see if does what you want

Standard Module

VBA Code:
Sub FormatTextBox(ByVal objTextBox As Object)
    Dim v           As Variant
    Select Case Len(objTextBox.Value)
        Case 1
            objTextBox.Value = Format(objTextBox.Value, "$0\.00")
        Case Is > 1
            v = Replace(objTextBox.Value, "$", "")
            v = Replace(v, ".", "")
            objTextBox.Value = Format(v / 100, "$#,#0.00")
        Case Else
    End Select
End Sub

Call from your textbox change events & pass the Textbox object as an argument

VBA Code:
Private Sub TextBox1_Change()
    FormatTextBox Me.TextBox1
End Sub

Dave
 
Upvote 1
Solution
@dmt32, I suspect you have more experience with forms than I have but this seems to work for me, unless you can see a downside.
VBA Code:
Call FormatTextBox(Me.ActiveControl)

@Alex Blakenburg

When I post answers for OPs I try & make my intentions as clear as possible (although I do not always succeed) in this case, specifying the actual control .
I like your suggestion - Apart from Call statement not strictly necessary but I know many like to use it, I can see no downside with your suggestion.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,005
Members
452,542
Latest member
Bricklin

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