0% found this document useful (0 votes)
6 views

code 1

for excel vba

Uploaded by

Fadrul96
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

code 1

for excel vba

Uploaded by

Fadrul96
Copyright
© © All Rights Reserved
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 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 ToggleButton1_Click()


' Toggle visibility of Group1 based on ToggleButton1 state
Dim groupVisible As Boolean
groupVisible = ToggleButton1.Value ' True when "On", False when "Off"

' Clear TextBox6 and TextBox7 when Group1 is turned on


If groupVisible Then
TextBox6.Text = ""
TextBox7.Text = ""
End If

' Set visibility of Group1 components


Label10.Visible = groupVisible
Label11.Visible = groupVisible
TextBox6.Visible = groupVisible
TextBox7.Visible = groupVisible
End Sub

Private Sub UserForm_Initialize()


Dim i As Integer

' Set ToggleButton1 to "Off" mode


ToggleButton1.Value = False

' Hide Group1 components


Label10.Visible = False
Label11.Visible = False
TextBox6.Visible = False
TextBox7.Visible = False

' 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

Private Sub TextBox6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numbers (0-9), one decimal point, backspace, and + or - at the
start
If KeyAscii = 43 Or KeyAscii = 45 Then ' + or - symbol
If Len(TextBox6.Text) = 0 Then
' Allow if the symbol is at the start
Else
KeyAscii = 0 ' Prevent symbol after number
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
ElseIf KeyAscii = 8 Then
' Allow backspace
ElseIf (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Then
' Allow numbers and decimal point only if the symbol is already input
If Len(TextBox6.Text) = 0 And KeyAscii <> 43 And KeyAscii <> 45 Then
KeyAscii = 0 ' Prevent input if there's no + or - at the start
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
Else
KeyAscii = 0 ' Disallow all other characters
MsgBox "Please input numbers or decimal point, and start with + or -.",
vbExclamation, "Invalid Input"
End If
End Sub

Private Sub TextBox7_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)


' Allow only numbers (0-9), one decimal point, backspace, and + or - at the
start
If KeyAscii = 43 Or KeyAscii = 45 Then ' + or - symbol
If Len(TextBox7.Text) = 0 Then
' Allow if the symbol is at the start
Else
KeyAscii = 0 ' Prevent symbol after number
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
ElseIf KeyAscii = 8 Then
' Allow backspace
ElseIf (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Then
' Allow numbers and decimal point only if the symbol is already input
If Len(TextBox7.Text) = 0 And KeyAscii <> 43 And KeyAscii <> 45 Then
KeyAscii = 0 ' Prevent input if there's no + or - at the start
MsgBox "Please input + or - symbol at the beginning.", vbExclamation,
"Invalid Input"
End If
Else
KeyAscii = 0 ' Disallow all other characters
MsgBox "Please input numbers or decimal point, and start with + or -.",
vbExclamation, "Invalid Input"
End If
End Sub

You might also like