Class Module & KeyDown

Magic_Doctor

Board Regular
Joined
Mar 18, 2009
Messages
56
Hello,

On the sheet there are several TextBoxes: "TextBoxPP1", "TextBoxPP2" ...
Each TextBox has its own procedure in the sheet module:
VBA Code:
Private Sub TextBoxPP1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Dim x As Byte, obj1 As Object, obj2 As Object, obj3 As Object, i As Byte, pourcent, ad

    x = 1
    Set obj1 = ActiveSheet.OLEObjects("TextBoxPP" & x).Object
    Set obj2 = ActiveSheet.OLEObjects("CheckBoxPP" & x).Object
    Set obj3 = ActiveSheet.OLEObjects("TextBox_AddPourcent").Object

    With obj1
        If KeyCode = 110 Then
            .Value = .Value & ","
            KeyCode = 0
            Exit Sub
        ElseIf KeyCode = vbKeyClear Or KeyCode = vbKeyDelete Then
            .Value = 0
            .Value = Format(CDbl(obj1.Value), "##,##0.00""%""")
            KeyCode = 0
            obj2 = 0
            CheckSolvants = CheckSolvants - 1
            pourcents(x) = 0
        End If
        If KeyCode = 13 Then
            With obj1
                If Not IsNumeric(.Value) Then
                    .Value = 0
                    .Value = Format(CDbl(obj1.Value), "##,##0.00""%""")
                    obj2 = 0
                    CheckSolvants = CheckSolvants - 1
                    pourcents(x) = 0
                    Exit Sub
                End If
                .Value = Replace(.Value, ".", ",")
                If CDbl(.Value) > 0 Then
                    pourcent = CDbl(obj1.Value)
                    pourcents(x) = pourcent
                    .Value = Format(CDbl(obj1.Value), "##,##0.00""%""")
                Else
                    .Value = 0
                    .Value = Format(CDbl(obj1.Value), "##,##0.00""%""")
                    obj2 = 0
                    CheckSolvants = CheckSolvants - 1
                    pourcents(x) = 0
                End If
            End With
            [A1].Select
        End If
        For i = 1 To NbSolvants + 1  '(Base 0)
            Set obj1 = ActiveSheet.OLEObjects("TextBoxPP" & i).Object
            ad = ad + pourcents(i)
        Next
        If ad > 100 Then
            pourcent = 100 - ad + pourcent
            pourcents(x) = pourcent
            Set obj1 = ActiveSheet.OLEObjects("TextBoxPP" & x).Object
            obj1.Value = pourcent
            obj1.Value = Format(CDbl(obj1.Value), "##,##0.00""%""")
        End If
        
        ad = 0
        For i = 1 To NbSolvants + 1  '(Base 0)
            Set obj2 = ActiveSheet.OLEObjects("CheckBoxPP" & i).Object
            If obj2 = -1 Then ad = ad + pourcents(i)
        Next
        obj3.Value = Format(ad, "##,##0.00""%""")
    End With
End Sub
The procedures are identical for all TextBoxes, the only difference is the value of the variable x at the start of the procedure: x = 1 for the TextBox "TextBoxPP1", x = 2 for the TextBox "TextBoxPP2" ...
I report that everything is working very well. The problem is, there are 20 TextBoxes ("TextBoxPP1" to "TextBoxPP20"), and since each procedure is relatively long, it's a never-ending story ... Also, I would like to handle all of these TextBox by means of a class module.
I started the work by writing:
1 / in "ThisWorkbook":
VBA Code:
Option Explicit
Dim TXB() As New Classe_ActiveX

Sub Workbook_Open()
    With Worksheets("Données")
        For Each obj In .OLEObjects
            If TypeName(obj.Object) = "TextBox" And ExtractText(obj.Name) = "TextBoxPP" Then
                ReDim Preserve TXB(n)
                Set TXB(n).TXB = obj.Object
                n = n + 1
            End If
        Next
    End With
End Sub
2 / in the class module that I called "Class_ActiveX":
VBA Code:
Option Explicit
Public WithEvents TXB As MSForms.TextBox

            and there I do not know how to do ...
I am unable to terminate my application due to "TXB_KeyDown". If it was "TXB_Change", that wouldn't be a problem.
Thanks in advance for helping me solve this problem.
 
A subsidiary question.
If we see square things, is it appropriate to add, at the end of "If KeyCode = 13 Then" "KeyCode = 0"?
VBA Code:
            If KeyCode = 13 Then
                If Not IsNumeric(.Value) Then
                    .Value = Format(0, "##,##0.00""%""")
                    obj = 0
                    CheckSolvants = CheckSolvants - 1
                    pourcents(x) = 0
                    Exit Sub
                End If
                .Value = Replace(.Value, ".", ",")
                If CDbl(.Value) > 0 Then
                    pourcent = CDbl(.Value)
                    pourcents(x) = pourcent
                    .Value = Format(pourcent, "##,##0.00""%""")
                Else
                    .Value = Format(0, "##,##0.00""%""")
                    obj = 0
                    CheckSolvants = CheckSolvants - 1
                    pourcents(x) = 0
                End If
                KeyCode = 0
                [A1].Select
            End If
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Our posts have crossed paths.
At the maximum of the maximum, I will not use more than 15 solvents. I think that even in the most sophisticated formulations (perfumes, cosmetics, etc.) we go above and beyond.
 
Upvote 0
As for:
VBA Code:
        If KeyCode = 110 Then
            .Value = .Value & ","
            KeyCode = 0
            Exit Sub
        ElseIf KeyCode = vbKeyClear Or KeyCode = vbKeyDelete Then
            .Value = 0
            .Value = Format(CDbl(obj1.Value), "##,##0.00""%""")
            KeyCode = 0
            obj2 = 0
            CheckSolvants = CheckSolvants - 1
            pourcents(x) = 0
        End If
 
Upvote 0
Yep. In your example you may omit the Exit Sub since it branches to the End If afterwards, so the ElseIf is never been executed in case of KeyCode equals 110.

EDIT: ... unless there's some code beneath the End If that you don't want to run in case of Key 110 ...
 
Upvote 0
Thanks GWteB for this new precision. Thanks to you, the procedure has improved tremendously.
I would need your help again, but I prefer to open a new thread, because the topic is different, even though it is very close to this one.
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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