Hello, I have been trying to work on a solution for a week and a come close but no cigar. I have a user form that the user enter's in their %'s for specific fields. Not all textboxes will have a %. What I'm trying to do is not allow the sum of all textboxes to exceed 100%, if it does, all textboxes are set to null & the user has to reenter. The problem is, bad data is still pasting to the spreadsheet so I thought I could clear the table that the information is being entered into. The problem is more than the last row on the table is being deleted and incorrect values are being placed in the textboxes. Below is my code, if anyone can direct me where I'm screwing up it will be greatly appreciated:
Again, any help will be greatly appreciated.
Code:
Dim cCont As Control
Dim sEntries As String
Dim oLst As ListObject
On Error Resume Next
' Write data to worksheet
Set ws = Worksheets("CoreConSummary")
LRow = ws.Cells(Rows.Count, 34).End(xlUp).Offset(0, 34).Row + 1
With ws
.Cells(LRow, 34).Value = Me.txt_SiteWrk.Value
.Cells(LRow, 35).Value = Me.txt_VESTIBULE.Value
.Cells(LRow, 36).Value = Me.txt_CHKOUT.Value
.Cells(LRow, 37).Value = Me.txt_PHOTOLAB.Value
.Cells(LRow, 38).Value = Me.txt_MINCLINIC.Value
.Cells(LRow, 39).Value = Me.txt_RETAIL.Value
.Cells(LRow, 40).Value = Me.txt_SOA.Value
.Cells(LRow, 41).Value = Me.txt_PHARMACY.Value
.Cells(LRow, 42).Value = Me.txt_EMPAREA.Value
.Cells(LRow, 43).Value = Me.txt_RECVAREA.Value
.Cells(LRow, 44).Value = Me.txt_RRMS.Value
.Cells(LRow, 45).Value = Me.txt_GENCOND.Value
.Cells(LRow, 46).Value = Me.txt_PROFIT.Value
.Cells(LRow, 47).Value = Format(Now, "mm/dd/yyyy hh:nn:ss")
End With
' Check that sum does not exceed 100%
For Each cCont In UserInput.Controls
If TypeOf cCont Is MSForms.TextBox Then _
sEntries = Val(txt_SiteWrk.Value) + Val(txt_VESTIBULE.Value) + Val(txt_CHKOUT.Value) + Val(txt_PHOTOLAB.Value) + Val(txt_MINCLINIC.Value) + Val(txt_RETAIL.Value) + Val(txt_SOA.Value) + Val(txt_PHARMACY.Value) + Val(txt_EMPAREA.Value) + Val(txt_RECVAREA.Value) + Val(txt_RRMS.Value) + Val(txt_GENCOND.Value) + Val(txt_PROFIT.Value) & CStr(cCont)
If Trim(sEntries) < 100 And Trim(sEntries) > 100 Then _
sEntries = False
MsgBox "Total percentage can not be less than OR exceed 100%, please check your entries and update", vbOKOnly + vbCritical, "ERROR!"
txt_SiteWrk = ""
txt_VESTIBULE = ""
txt_CHKOUT = ""
txt_PHOTOLAB = ""
txt_MINCLINIC = ""
txt_RETAIL = ""
txt_SOA = ""
txt_PHARMACY = ""
txt_EMPAREA = ""
txt_RECVAREA = ""
txt_RRMS = ""
txt_GENCOND = ""
txt_PROFIT = ""
'Clear table on spreadsheet to reapply correct %
Application.ScreenUpdating = False
If ActiveSheet.ListObjects.Count > 1 Then
For Each oLst In ActiveSheet.ListObjects
With oLst
If .Name = "PERCENTAPP" Then
If oLst.ListRows.Count > 1 Then
oLst.ListRows(oLst.ListRows.Count).Delete
End If
End If
End With
Next
End If
Next
txt_SiteWrk.SetFocus
' Clear the form
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
ctl.Value = ""
ElseIf TypeName(ctl) = "CheckBox" Then
ctl.Value = False
End If
Next ctl
End Sub
Again, any help will be greatly appreciated.