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:
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":
2 / in the class module that I called "Class_ActiveX":
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.
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
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
VBA Code:
Option Explicit
Public WithEvents TXB As MSForms.TextBox
and there I do not know how to do ...
Thanks in advance for helping me solve this problem.