Excel VBA displaying currency with Afterupdate and Change events - excel

I created a userform data base where I can both add new data and search for current project details.
There are certain textboxes where I would like for the following:
When adding data, for amount to display as currency.
I already used the Afterupdate() event and it works well,
Private Sub txtPOAmount_Afterupdate()
txtPOAmount.Value = Format(txtPOAmount.Value, "$#,###.##")
End Sub
I would also like it to display currency when it pulls the data
I used the Change() event, which also does the job
Private Sub txtPOAmount_Change()
txtPOAmount.Value = Format(txtPOAmount.Value, "$#,##0.00")
End Sub
Current issues with using one or the other:
-When I use the Afterupdate() event if I were to pull data already in the data base it doesn't show the currency unless I update it.
-When I use the Change() event only, it displays the data as currency, but when I want to update the data only the fist number typed in works. (ex. Type 5337, it displays $5.00)
However, I would like for both of this options to work simultaneously. If I want to pull data then I would like the userform to display currency, and if I update it, I would like for it to enter the complete amount.

Try using TextBox1_Exit to update it instead of the Change() event, that means you get freedom to edit more than one character in the textbox string before the event gets fired and stops you having to use arrow keys to move left and right in your "currency" textbox:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.Value = Format(TextBox1.Value, "£#,##0.00")
End Sub

Related

Is there a why a userform _change procedureal event not be recursive?

I have data in a textbox (call it info) on a userform that I would like the user to be able to change so I set up a info_change() subroutine. Problems are:
(1) as I load initial data into info.text from VBA, the info_change() subroutine is called.
(2) when I go into the info field to change the value INSIDE the info field, it call the info_change() subroutine again and continues to call the routine until the last entry I put in info field = value in info field before changing in (seems recursive)
Any thoughts? Maybe instead of calling it info_change(), call it another procedural event?
Thanks, Marty
Easy example using _AfterUpdate event
Instead of reacting each time a single part of the info textbox got changed by the control's _Change event, it's more efficient to use the _AfterUpdate event pausing till you changed to another control (btw could be easily augmented by a CommandButton procedure).
In order to prevent unnecessary calls a comparison is made between old and current info text using a type definition in the code module's declaration head. Furthermore I added a boolean IsTest variable to this definition to allow a test display of changes made triggering some other stuff in code (see b).
Additional hint: there's no recursion, the Change event or AfterUpdate event are only firing as reaction to each single change or update, i.e. making no difference if triggered via Userform_Initialize settings or an eventual user input, however the condition check prevents the Init from getting active.
Declaration head of Userform code module
Option Explicit ' declaration head of code module
'User defined type
Private Type TThis
cnt As Long
oldTxt As String
IsTest As Boolean
End Type
Dim this As TThis
Private Sub UserForm_Initialize()
'[1]assign text to Info-Control
Me.TextBox1.Text = "bla bla"
'[2]remember text
this.oldTxt = Me.TextBox1.Text
'[3]optional choice of test modus
this.IsTest = True
If this.IsTest Then Debug.Print " #", "Change history", "(new) text" & vbNewLine; 0, Now, this.oldTxt
End Sub
Private Sub TextBox1_AfterUpdate()
'~~~~~~~~~~~~~~~
'~> Example call
'~~~~~~~~~~~~~~~
check Me.TextBox1
End Sub
Sub check(myTextbox As MSForms.TextBox)
' Purp.: compare current text with old text
If this.oldTxt <> myTextbox.Text Then
'a) optional test display
If this.IsTest Then this.cnt = this.cnt + 1: Debug.Print this.cnt, Now, myTextbox.Text
'b) do other stuff
' ...
'c) remember old text
this.oldTxt = myTextbox.Text
End If
End Sub

VBA Excel : Populate TextBox with specific string of text when Option Button is selected

I developed a Form in Excel (2016) and I am trying (with VBA) to configure its behavior so that if the user selects a particular option button, two additional things happen:
A checkbox further down on the form is automatically checked.
A text box further down on the form automatically displays a set string of text.
More specifically, if the user selects OptionButtonABC, then ...
CheckBoxNone should become checked
TextBoxCompanyName (which does not display any text by default) should now display the string: 'ABC'.
I initially created a subroutine that just targeted condition 1, and everything worked fine. However, when I try to integrate the code required to handle condition 2, things start to unravel.
Below, you will see the code in its most current state. I have a Private Sub that initiates upon the Click event and then immediately defines a variable as a string. I then set up an if/then statement that specifies what should happen IF OptionButtonABC is true. Namely, CheckBoxNone should be selected (this works fine) AND TextBoxCompanyName should now display the string 'ABC'.
Private Sub OptionButtonABC_Click()
Dim Variable_ABC As String
Variable_ABC = ABC
If Me.OptionButtonABC.Value = True Then
Me.CheckBoxNone = True And Me.TextBoxCompanyName.Text = Variable_ABC
End If
End Sub
The desired behavior should (theoretically) be pretty easy to achieve, but my experience with VBA is still pretty limited, so I am reaching out to the community for a bit of advice. As I mentioned above, the code above works for the first condition. However, I am still unable to get the string of text ('ABC') to show up in the text box.
Thanks in advance for any advice you may offer.
Private Sub OptionButtonABC_Click()
Dim Variable_ABC As String
Variable_ABC = "ABC" 'String Values uses double quotes
If Me.OptionButtonABC.Value = True Then
Me.CheckBoxNone = True
Me.TextBoxCompanyName.Text = Variable_ABC
End If
End Sub
The operator AND must be used only in the IF statement comparison, not in what you want to do.

VBA best practices for modules relative to modeless userforms

I came across this similar issue and read the replies: Modeless form that still pauses code execution
I have been attempting to apply in my own situation the suggestion provided by David Zemens. In my situation, I cannot seem to find an approach that incorporates Mr. Zemen's suggestion without also utilizing a GoTo.
I am wondering if there is a better or more elegant solution.
Here is an outline of what I am doing:
I have a UserForm with a Command Button that begins the code execution that will perform several actions on multiple Excel workbooks. As such, there are a number of blocks of code and the successful completion of one block of code allows for the execution of the subsequent block of code.
At a certain point, depending on the situation, the code might require User input; in other situations, the needed data is obtainable from an Excel. If input is needed from the User, another UserForm is displayed.
The User may need to view several different Excel sheets before entering the input, so the UserForm is modeless. So the code comes to a stop until the User enters the needed input and clicks another Command Button.
It is at this point I am having trouble: how to resume the program flow. Is the only way to 'pick-up where it left-off' is by using a GoTo statement? Or is there some way to organize the modules so there is a single consistent program flow, defined in one spot and not duplicated from the point at which User input might be needed?
Here is my take on the problem . Hope I understood the problem correctly.
Assumptions:
There are two user forms.
UserForm1 with a button to start the processing.
UserForm2 with a button to supply intermediate input.
A sub inside a module to start/ launch UserForm1.
VBA Code (for the sub routine)
Sub LaunchUserForm1()
Dim frm As New UserForm1
'/ Launch the main userform.
frm.Show vbModeless
End Sub
VBA Code (for UserForm1)
Private Sub cmdStart_Click()
Dim i As Long
Dim linc As Long
Dim bCancel As Boolean
Dim frm As UserForm2
'/ Prints 1 to 5 plus the value returned from UserForm2.
For i = 1 To 5
If i = 2 Then
Set frm = New UserForm2
'/ Launch supplementary form.
frm.Show vbModeless
'<< This is just a PoC. If you have large number of inputs, better way will be
' to create another prop such as Waiting(Boolean Type) and then manipulate it as and when User
' supplies valid input. Then validate the same in While loop>>
'/ Wait till we get the value from UserForm2.
'/ Or the User Cancels the Form with out any input.
Do While linc < 1 And (linc < 1 And bCancel = False)
linc = frm.Prop1
bCancel = frm.Cancel
DoEvents
Loop
Set frm = Nothing
End If
Debug.Print i + linc
Next
MsgBox "User Form1's ops finished."
End Sub
VBA Code (for UserForm2)
Dim m_Cancel As Boolean
Dim m_prop1 As Long
Public Property Let Prop1(lVal As Long)
m_prop1 = lVal
End Property
Public Property Get Prop1() As Long
Prop1 = m_prop1
End Property
Public Property Let Cancel(bVal As Boolean)
m_Cancel = bVal
End Property
Public Property Get Cancel() As Boolean
Cancel = m_Cancel
End Property
Private Sub cmdlinc_Click()
'/Set the Property Value to 10
Me.Prop1 = 10
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'/ Diasble X button
Me.Cancel = True
Me.Hide
Cancel = True
End Sub
OK so here are my thoughts.
You have a userform frmSelectUpdateSheet which you wish to use in order to allow the user to select the sheet, when the sheet can't be determined programmatically. The problem is that if you do .Show vbModeless (which allows the user to navigate the worksheet/s), then code continues to execute which either leads to errors or otherwise undesired output.
I think it's possible to adapt the method I described in the previous answer. However, that's out of the question here unless you're paying me to reverse engineer all of your code :P
Assuming you have a Worksheet object variable (or a string representing the sheet name, etc.) which needs to be assigned at this point (and that this variable is Public in scope), just use the CommandButton on the form to assign this based on the selected item in the frmSelectUpdateSheet list box.
This is probably a superior approach for a number of reasons (not the least of which is trying to avoid application redesign for this sort of fringe case), such as:
This keeps your form vbModal, and does prevent the user from inadvertently tampering with the worksheet during the process, etc.
Using this approach, the thread remains with the vbModal displayed frmSelectUpdateSheet, and you rely on the form's event procedures for control of process flow/code execution.
It should be easier (and hence, cheaper) to implement; whether you're doing it yourself or outsourcing it.
It should be easier (and hence, cheaper) to maintain.
NOW, on closer inspection, it looks like you're already doing this sort of approach with the cmdbtnSelect_Click event handler, which leads me to believe there's a related/follow-up problem:
The sheet names (in listbox) are not sufficient for user to identify the correct worksheet. So if the user needs the ability to "scroll" the sheet (e.g., to review data which does not fit in the window, etc.), then add some spinner buttons or other form controls to allow them to navigate the sheet.

Automatically add to list

I'm having a small issue manipulating data.
I have a few check-boxes to select various items on the screen, which in turn inputs relevant data such as item name, price, etc. The issue is that these names have to go onto a invoice, and I cannot get the names to transfer onto the invoice nicely.
The invoice has to record the items, but it needs to check to see if a item has been already applied to the invoice and this is what I'm having problems with.
How can I get items to collectively display on a sheet without any additional interactions (I just click the relevant checkbox instead of manually typing it in), and display them nicely.
The items 'Underlay and Extended Warranty' need to be displayed one after another, and not 2-3 boxes away.
Example:
Underlay Extended Warranty
I don't want this:
Underlay
Extended Warranty
Is there a solution to this?
Update:
This happens if I do it the way I know:
I solved this by making a custom Excel VBA Macro that basically tells us the cell that is free which is closest to the top of the invoice:
Public Function GetApplicableCell() As String
Range("C20").Select
If (IsEmpty(Range("'Invoice Sheet'!C20").Value)) Then
GetApplicableCell = "C20"
End If
If (IsEmpty(Range("'Invoice Sheet'!C21").Value)) Then
GetApplicableCell = "C21"
End If
If (IsEmpty(Range("'Invoice Sheet'!C22").Value)) Then
GetApplicableCell = "C22"
End If
If (IsEmpty(Range("'Invoice Sheet'!C23").Value)) Then
GetApplicableCell = "C23"
End If
If (IsEmpty(Range("'Invoice Sheet'!C24").Value)) Then
GetApplicableCell = "C24"
End If
End Function
It can be called by a =GetApplicableCell() from within the worksheet, and we can work with this to produce a workable solution.

Lock Select Box Size In Excel

This seems like a simple thing, but I cannot figure it out, or find it online.
If I select 5 cells in a column(say A1:A5), and I would like to move this selection shape(column 1:5) over (to B1:B5); Is there shortcut to do this? Currently I hit the left arrow, and the select box changes size to just B1, and I have to hit shift and select B2:B5. Ideally I would like to discover a hot key that "locks" the shape of the select box.
It has been suggested by colleagues to write a macro, but this is ineffective in many cases. For example what if instead of a column I wanted to do the same thing with a row, or with a different sized shape. It seems likely that excel has this feature built in.
I'm not sure how a macro would be ineffective. I would write procedures similar to what's below, then assign them to hotkeys. Let me know if it works for you.
Option Explicit
Public rowDim As Long
Public colDim As Long
Public putSel as Boolean
Sub togglePutSel()
putSel = Not putSel
End Sub
Sub GetSelShape()
rowDim = Selection.Rows.Count
colDim = Selection.Columns.Count
putSel = True
End Sub
Sub PutSelShape()
Selection.Resize(rowDim, colDim).Select
End Sub
If you want to make it work for whenever you hit the arrow keys, then in your Sheet code, you can use this. You may want to do a quick check that rowDim and colDim aren't 0. The only issue with this is that you'd be stuck with that behavior unless you create a trigger to stop calling PutSelShape. So, I'd suggest one macro (hotkeyed to GetSelShape) to toggle it, and another hotkey for togglePutSel.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If putSel Then
Call PutSelShape
End If
End Sub

Resources