Userform Textbox display format help

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
I have this following userform that displays the GL account in gray before the user inputs an amount:

Picture1.jpg


However, when this form launches, the cursor is in the first cell which then causes the format to switch to the format for after input has been received. Is there any way to have all three cells appear as the last two do? Here is my code:
VBA Code:
Private Sub Userform_Initialize()
    Me.Top = Application.Top + (Application.UsableHeight / 2) - (Me.Height / 2)
    Me.Left = Application.Left + (Application.UsableWidth / 2) - (Me.Width / 2)
    
    For a = 1 To 3
        Me.Controls("txtAmount" & a).ForeColor = RGB(166, 166, 166)
        Me.Controls("txtAmount" & a).TextAlign = fmTextAlignLeft
    Next
    
    Me.txtAmount1 = "GL A/C 7833"
    Me.txtAmount2 = "GL A/C 7832"
    Me.txtAmount3 = "GL A/C 7831"
    
End Sub

Here is the code for after the cell is updated:
VBA Code:
Private Sub txtAmount1_AfterUpdate()
    Dim txt As Object
    Set txt = Me.txtAmount1
    
    txt.ForeColor = RGB(0, 0, 0)
    txt.TextAlign = fmTextAlignRight
    
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
    End With
    
    If txt.Value = "" Then
        txt.TextAlign = fmTextAlignLeft
        txt.ForeColor = RGB(166, 166, 166)
        txt.Value = "GL A/C 7833"
    End If
End Sub

I know this is a minor UX issue but this project needs to be as polished as possible. Thanks for any help in advance!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,
try adding a boolean flag to TOP of your forms code page - this is then used to exit the event before form is displayed

Rich (BB code):
Dim EventsOff As Boolean

Private Sub Userform_Initialize()

   EventsOff = True

    Me.Top = Application.Top + (Application.UsableHeight / 2) - (Me.Height / 2)
    Me.Left = Application.Left + (Application.UsableWidth / 2) - (Me.Width / 2)
    
    For a = 1 To 3
        Me.Controls("txtAmount" & a).ForeColor = RGB(166, 166, 166)
        Me.Controls("txtAmount" & a).TextAlign = fmTextAlignLeft
    Next
    
    Me.txtAmount1 = "GL A/C 7833"
    Me.txtAmount2 = "GL A/C 7832"
    Me.txtAmount3 = "GL A/C 7831"

End Sub

Private Sub txtAmount1_AfterUpdate()
    Dim txt As Object
    
    If EventsOff Then EventsOff = False: Exit Sub
    
    Set txt = Me.txtAmount1
    
    txt.ForeColor = RGB(0, 0, 0)
    txt.TextAlign = fmTextAlignRight
    
    With txt
        If IsNumeric(.Value) Then .Value = Format(.Value, "#,##0.00")
    End With
    
    If txt.Value = "" Then
        txt.TextAlign = fmTextAlignLeft
        txt.ForeColor = RGB(166, 166, 166)
        txt.Value = "GL A/C 7833"
    End If
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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