View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sam Kuo[_3_] Sam Kuo[_3_] is offline
external usenet poster
 
Posts: 86
Default Textbox question

Hi

I have 3 textboxes in UserForm where user inputs value and the sum must
always add up to 100. If any 2 textboxes have valid input, the third will be
filled in automatically.

Here is my attempt using nested IF, but I doubt I'm declaring my textboxes
or used the "IsEmpty" function correctly here because type mismatch error
occurs...Can someone please help me making it work?

Note: The following code relates to first textbox (txtSilt) only. Codes for
the other 2 textboxes are similar and I'll post them if it helps.

Private Sub txtSilt_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

' Declare 3 textboxes (txtSilt, txtSand, txtClay)
Dim NumtxtSilt As Variant, NumtxtSand As Variant, NumtxtClay As Variant
NumtxtSilt = Me.txtSilt.Value
NumtxtSand = Me.txtSand.Value
NumtxtClay = Me.txtClay.Value

' If the input is NOT numerical, show warning label,
' show and copy null value to worksheet, by calling sub CopySiltEmpty
If Not IsNumeric(NumtxtSilt) Then
Me.labSiltWarning.Visible = True
Me.labSiltWarning.Caption = "<-- You must enter a NUMBER"
CopySiltEmpty

' Or if the sum of Silt and Sand is within 100%,
' hide warning label, show and copy both Silt and calculated Clay
value
' to worksheet, by calling sub CopySilt and sub CopyClay
ElseIf Not IsEmpty(NumtxtSilt) And Not IsEmpty(NumtxtSand) And
NumtxtSilt + NumtxtSand <= 100 Then
NumtxtClay = 100 - NumtxtSilt - NumtxtSand
CopySilt
CopyClay

' Or if the sum of Silt and Sand exceeds 100%,
' show warning label, show and copy empty value to worksheet
' by calling sub CopySiltEmpty
ElseIf Not IsEmpty(NumtxtSilt) And Not IsEmpty(NumtxtSand) And
NumtxtSilt + NumtxtSand 100 Then
Me.labSiltWarning.Visible = True
Me.labSiltWarning.Caption = "<-- Total soil composite percentage
must add up to 100%"
CopySiltEmpty

' Or if the sum of Silt and Clay is within 100%,
' show and copy both Silt and calculated Sand values to worksheet
' by calling sub CopySilt and sub CopySand
ElseIf Not IsEmpty(NumtxtSilt) And Not IsEmpty(NumtxtClay) And
NumtxtSilt + NumtxtClay <= 100 Then
NumtxtSand = 100 - NumtxtSilt - NumtxtClay
CopySilt
CopySand

' Or if the sum of Silt and Clay exceeds 100%,
' show warning label, show and copy empty value to worksheet
' by calling sub CopySiltEmpty
ElseIf Not IsEmpty(NumtxtSilt) And Not IsEmpty(NumtxtClay) And
NumtxtSilt + NumtxtClay 100 Then
Me.labSiltWarning.Visible = True
Me.labSiltWarning.Caption = "<-- Total soil composite percentage
must add up to 100%"
CopySiltEmpty

' Or if the input is outside 0 and 100, show warning label,
' show and copy empty value to worksheet
' by calling sub CopySiltEmpty
ElseIf NumtxtSilt < 0 Or NumtxtSilt 100 Then
Me.labSiltWarning.Visible = True
Me.labSiltWarning.Caption = "<-- Please enter a number between 0
and 100"
CopySiltEmpty

' Else hide warning label, show and copy data to worksheet
' by calling sub CopySilt
Else
CopySilt

End If

End Sub

' Below is stored in Module 1
Sub CopySiltEmpty()

Dim ws As Worksheet
Dim rngSilt As Range
Dim NumtxtSilt As Variant

Set ws = ThisWorkbook.Worksheets("Form Control 1")
Set rngSilt = ws.Range("B4")

' Clear input and change textbox BackColor to yellow
NumtxtSilt = frmUSLE.txtSilt.Value
NumtxtSilt = Empty
frmUSLE.txtSilt.Value = Empty
frmUSLE.txtSilt.BackColor = &HFFFF&

' Copy empty value to the worksheet
rngSilt.Value = Empty

End Sub

' Below is stored in Module 1
Sub CopySilt()

Dim ws As Worksheet
Dim rngSilt As Range

Set ws = ThisWorkbook.Worksheets("Form Control 1")
Set rngSilt = ws.Range("B4")

' Set textbox format to one decimal point
frmUSLE.txtSilt.Text = Format(frmUSLE.txtSilt.Text, "#,##0.0")

' Copy the data to the worksheet
rngSilt.Value = frmUSLE.txtSilt.Value

' Hide warning label, change textbox BackColor to white
frmUSLE.labSiltWarning.Visible = False
frmUSLE.txtSilt.BackColor = &H80000005

End Sub