0% found this document useful (0 votes)
12 views4 pages

code 2

Uploaded by

Fadrul96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

code 2

Uploaded by

Fadrul96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

' Declare a module-level variable to store the previously selected ComboBox1 item

Dim previousComboSelection As String

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numbers (0-9), one decimal point, and backspace
Select Case KeyAscii
Case 8 ' Backspace
' Allow
Case 46 ' Decimal point
If InStr(1, TextBox1.Text, ".") > 0 Then
KeyAscii = 0 ' Disallow more than one decimal point
End If
Case 48 To 57 ' Numbers 0-9
' Allow
Case Else
KeyAscii = 0 ' Disallow all other characters
MsgBox "Please enter only numbers or a decimal point. Alphabets and
special characters are not allowed.", vbExclamation, "Invalid Input"
End Select
End Sub

Private Sub TextBox1_Change()


Dim decimalPlaces As Integer
Dim textValue As String
Dim tolerance As String
Dim diameterSymbol As String
Dim currentComboData As String

' Get the value from TextBox1


textValue = TextBox1.Text

' Count the number of decimal places


If InStr(textValue, ".") > 0 Then
decimalPlaces = Len(Mid(textValue, InStr(textValue, ".") + 1))
Else
decimalPlaces = 0 ' No decimal places
End If

' Set tolerance based on decimal places


Select Case decimalPlaces
Case 0
tolerance = "± 0.5"
Case 1
tolerance = "± 0.25"
Case 2
tolerance = "± 0.1"
Case 3
tolerance = "± 0.05"
Case Else
tolerance = "" ' No tolerance if more than 3 decimal places
End Select

' Display the tolerance in TextBox3


TextBox3.Text = tolerance

' Calculate sum for TextBox4 (addition)


If tolerance <> "" Then
' Extract the numerical part from TextBox3 (removing "± " prefix)
Dim toleranceValue As Double
toleranceValue = Val(Mid(tolerance, 3))

' Perform the calculation (TextBox1 + TextBox3 value)


TextBox4.Text = TextBox1.Value + toleranceValue
Else
' If TextBox3 is empty, clear TextBox4
TextBox4.Text = ""
End If

' Calculate difference for TextBox5 (subtraction)


If tolerance <> "" Then
' Perform the calculation (TextBox1 - TextBox3 value)
TextBox5.Text = TextBox1.Value - toleranceValue
Else
' If TextBox3 is empty, clear TextBox5
TextBox5.Text = ""
End If

' Retain "Ø " if it exists at the start of TextBox2


diameterSymbol = "Ø"
If Left(TextBox2.Text, Len(diameterSymbol) + 1) = diameterSymbol & " " Then
TextBox2.Text = diameterSymbol & " " & TextBox1.Text
Else
' Retain ComboBox1 data (if any) while updating TextBox2 with TextBox1 data
If previousComboSelection <> "" Then
currentComboData = " " & previousComboSelection
Else
currentComboData = ""
End If
TextBox2.Text = TextBox1.Text & currentComboData
End If
End Sub

Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Validate input for TextBox6
Call ValidateTextBoxSymbol(KeyAscii, TextBox6)
End Sub

Private Sub TextBox7_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Validate input for TextBox7
Call ValidateTextBoxSymbol(KeyAscii, TextBox7)
End Sub

Private Sub ValidateTextBoxSymbol(ByVal KeyAscii As MSForms.ReturnInteger, ByRef


textBox As MSForms.TextBox)
' Allow only numbers, one decimal point, and + or - at the start
If Len(textBox.Text) = 0 Then
' First character must be + or -
If KeyAscii <> 43 And KeyAscii <> 45 Then ' + or -
KeyAscii = 0 ' Block input
textBox.Text = "" ' Clear the text box
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
Else
' Allow only numbers and one decimal point after the symbol
Select Case KeyAscii
Case 8 ' Backspace
' Allow
Case 46 ' Decimal point
If InStr(2, textBox.Text, ".") > 0 Then ' Allow only one decimal
point after the first character
KeyAscii = 0
textBox.Text = "" ' Clear the text box
MsgBox "Only one decimal point is allowed.", vbExclamation,
"Invalid Input"
End If
Case 48 To 57 ' Numbers 0-9
' Allow
Case Else
KeyAscii = 0 ' Disallow all other characters
textBox.Text = "" ' Clear the text box
MsgBox "Numerical values with decimal places only.", vbExclamation,
"Invalid Input"
End Select
End If
End Sub

Private Sub ComboBox1_Change()


Dim currentSelection As String
Dim textBoxContent As String

currentSelection = ComboBox1.Value

' Remove the previous ComboBox1 selection from TextBox2 (if any)
If previousComboSelection <> "" Then
textBoxContent = Replace(TextBox2.Text, " " & previousComboSelection, "")
Else
textBoxContent = TextBox2.Text
End If

' Append the new ComboBox1 selection at the end with a space
TextBox2.Text = textBoxContent & " " & currentSelection

' Update the previous selection


previousComboSelection = currentSelection
End Sub

Private Sub CommandButton1_Click()


Dim diameterSymbol As String
diameterSymbol = "Ø"

' Check if TextBox2 already starts with "Ø" followed by a space


If Left(TextBox2.Text, Len(diameterSymbol) + 1) = diameterSymbol & " " Then
MsgBox "Data already added", vbExclamation, "Duplicate Symbol"
Else
' Add "Ø" with a space at the beginning of the current data in TextBox2
TextBox2.Text = diameterSymbol & " " & TextBox2.Text
End If
End Sub

Private Sub UserForm_Initialize()


Dim i As Integer

' Populate ComboBox1 in order: f1 to f12, F1 to F12, h1 to h12, H1 to H12


For i = 1 To 12
ComboBox1.AddItem "f" & i
Next i
For i = 1 To 12
ComboBox1.AddItem "F" & i
Next i
For i = 1 To 12
ComboBox1.AddItem "h" & i
Next i
For i = 1 To 12
ComboBox1.AddItem "H" & i
Next i

' Lock TextBox2 to prevent user input


TextBox2.Locked = True

' Initialize the previous selection to an empty string


previousComboSelection = ""
End Sub

You might also like