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

VBA Input Validation and Calculation Guide

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)
22 views4 pages

VBA Input Validation and Calculation Guide

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

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

Dim previousComboSelection As String

Private Sub TextBox1_KeyPress(ByVal KeyAscii As [Link])


' Allow only numbers (0-9), one decimal point, and backspace
Select Case KeyAscii
Case 8 ' Backspace
' Allow
Case 46 ' Decimal point
If InStr(1, [Link], ".") > 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 = [Link]

' 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


[Link] = 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)


[Link] = [Link] + toleranceValue
Else
' If TextBox3 is empty, clear TextBox4
[Link] = ""
End If

' Calculate difference for TextBox5 (subtraction)


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

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


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

Private Sub TextBox6_KeyPress(ByVal KeyAscii As [Link])


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

Private Sub TextBox7_KeyPress(ByVal KeyAscii As [Link])


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

Private Sub ValidateTextBoxSymbol(ByVal KeyAscii As [Link], ByRef


textBox As [Link])
' Allow only numbers, one decimal point, and + or - at the start
If Len([Link]) = 0 Then
' First character must be + or -
If KeyAscii <> 43 And KeyAscii <> 45 Then ' + or -
KeyAscii = 0 ' Block input
[Link] = "" ' 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, [Link], ".") > 0 Then ' Allow only one decimal
point after the first character
KeyAscii = 0
[Link] = "" ' 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
[Link] = "" ' 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 = [Link]

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

' Append the new ComboBox1 selection at the end with a space
[Link] = 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([Link], 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
[Link] = diameterSymbol & " " & [Link]
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
[Link] "f" & i
Next i
For i = 1 To 12
[Link] "F" & i
Next i
For i = 1 To 12
[Link] "h" & i
Next i
For i = 1 To 12
[Link] "H" & i
Next i

' Lock TextBox2 to prevent user input


[Link] = True

' Initialize the previous selection to an empty string


previousComboSelection = ""
End Sub

You might also like