Search code examples
vbaexcelcommandbutton

Excel User Form - How to code the Command Button to enter a Value of True when Clicked and value of False when not clicked?


I'm new to VBA Programming but I have started to learn the basics. I am a Trader in the Futures Markets so I'm building a userform to assist with record-keeping.

My userform with a Save button seems to correctly populate the relevant cells. My problem is with the Command Buttons:

When clicked, the Save button will:

  1. Ensure the user completed all required fields
  2. When properly completed, it will add the data to the relevant cells in the next empty row.

Command buttons are named Long, Short, Yes, No, and buttons in the Filters Frame etc. I'm trying to get it so:

  • The relevant cell should contain FALSE until the button is clicked, then it should change to TRUE.

For example, if I click Long, there is no data on the worksheet currently, so D3 (under the Long Header) should be TRUE. Then, when I enter a different set of data, and do not click Long, it should show FALSE in perhaps D4 (the next row because data would be in the previous row...and so on).

I was unsuccessful at solving this on my own with online research. I'd appreciate help learning to code 1 command button, then the rest will be easy.

My naming convention is:

  • Text Boxes: tbMkt, tbEntryPrice, tbStopLoss, tbTargetPrice etc.

  • Buttons: btnLong, btnShort, btnYesTrend, btnNoTrend, btnFilter1, btnFilter2, btnFilter3.

Additional Information: Code

Option Explicit
Public myGlobalVar As Boolean
Private Sub btnClose_Click()
    Me.Hide
    Unload Me
End Sub
Private Sub btnLong_Click()
    myGlobalVar = True
End Sub

Private Sub btnSave_Click()

    Me.Hide


    If tbMkt.Value = "" Or tbEntry = "" Or tbStopLoss = "" Or tbTarget = "" Then
        If MsgBox("Form is not complete. Do you want to continue?", vbQuestion + vbYesNo) <> vbYes Then
        Exit Sub
        End If
    End If


    Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
    ActiveCell.Value = tbMkt.Value
    ActiveCell.Offset(0, 1).Value = tbDate.Value
    ActiveCell.Offset(0, 2).Value = tbTime.Value
    ActiveCell.Offset(0, 3).Value = btnLong.Visible

    ActiveCell.Offset(0, 5).Value = tbEntry.Value
    ActiveCell.Offset(0, 6).Value = tbStopLoss.Value
    ActiveCell.Offset(0, 7).Value = tbTarget.Value

    MsgBox "Thank You for entering the details of your trade. They have been added to row " & ActiveCell.Row
    Unload Me

    Call resetForm

    Me.tbDate = Date
    tbDate.Value = Format(tbDate.Value, "dd/mm/yyyy")
    Me.tbTime = Time

End Sub

Sub resetForm()

    tbMkt.Value = ""
    tbDate.Value = ""
    tbTime.Value = ""
    tbEntry.Value = ""


    tbStopLoss.Value = ""
    tbTarget.Value = ""
    tradeEntryUserForm.tbMkt.SetFocus

End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()

    Me.tbDate = Date
    tbDate.Value = Format(tbDate.Value, "dd/mm/yyyy")
    Me.tbTime = Time

End Sub

Private Sub WithTrendFrame_Click()

End Sub

I have declared myGlobalVar as per the above code. I have set this to True in the btnLong_Click() but I am unsure what you mean by step 2 loading the event set to False on the form? Please help.

Hope I'm doing the correct thing by editing to add more information around my question...

User Form:

User Form

Worksheet:

Excel worksheet


Solution

  • THis is really easy.

    1) Set a global variable of boolean type

    2) on form load event set to false

    3) on button click event set to true

    4) figure out where in the process the value should be reset if necessary

    ALso because you call a function called "Reset form" just be aware that if youre reloading the form, that per my description, if followed, that it will reset the value of the boolean variable (might be intentional but thats up to you to decide).