Option Explicit
Option Compare Text
Const MYTAG = "MyTag"
Public WithEvents UserFormCtl As clsWatchEvents
Private bFormUnloaded As Boolean
Private bCancel As Boolean
Dim intLastRow As Integer
Dim arrParts()
Private Sub UserForm_Initialize()
Dim i As Integer, ii As Integer, iii As Integer, iv As Integer, intArrayDim As Integer, intNLA As Integer
Dim intTypesOnHand As Integer, intPcsOnHand As Integer, intBought As Integer, intInstalled As Integer
Dim dblTotalCost As Double, dblBoughtCost As Double, dblNLACost As Double
Dim c As MSForms.Control
Application.ScreenUpdating = False
Sheets("Replacement Parts").Select
intLastRow = Range("B155").End(xlUp).Row
If intLastRow = 6 Then
intLastRow = 7
End If
intArrayDim = intLastRow - 6
Erase arrParts()
ReDim arrParts(1 To intArrayDim + 1, 1 To 8)
If intArrayDim = 150 Then
ReDim arrParts(1 To intArrayDim, 1 To 8)
End If
If 1 + (20 * UBound(arrParts)) > 221 Then
Frame1.ScrollHeight = 1 + (20 * UBound(arrParts))
End If
iii = 0
iv = 0
' ===== Step 1: Initialize array values from the worksheet and assign those values to textboxes on the form =====
For i = 1 To UBound(arrParts)
arrParts(i, 1) = Range("B" & i + 6)
Me.Controls("TextBox" & 1 + iv).Value = arrParts(i, 1)
Me.Controls("TextBox" & 1 + iv).Visible = True
For ii = 2 To 7
iii = 2 * (ii - 1)
arrParts(i, ii) = Range("B" & i + 6).Offset(0, iii)
Me.Controls("TextBox" & ii + iv).Value = arrParts(i, ii)
Me.Controls("TextBox" & ii + iv).Visible = True
Next ii
iv = iv + 7
Next i
' ===== Step 2: Calculate, format and display inventory data stats =====
'various calculations and label format steps follow
Application.ScreenUpdating = True
End Sub
Private Sub UserForm_Layout()
If UserFormCtl Is Nothing Then
Set UserFormCtl = New clsWatchEvents
UserFormCtl.StartWatching Me
End If
End Sub
Private Sub UserForm_Terminate()
UserFormCtl.StopWatching
Set UserFormCtl = Nothing
End Sub
Private Sub UserFormCtl_OnEnter(Ctrl As MSForms.Control)
'Do nothing
End Sub
Private Sub UserFormCtl_OnExit(Ctrl As MSForms.Control, Cancel As Boolean)
Const WARNMSG = "The installation date has changed. Do you wish to adjust the inventory?"
Dim i As Long, strTag As String
' Exit if Ctrl is not TextBox
If Not TypeOf Ctrl Is MSForms.TextBox Then Exit Sub
' Exit if Tag of control is not 1, the tag given to the 7th textbox in each row
If Not Ctrl.Tag = 1 Then Exit Sub
' Compare value of TextBox with arrParts(i,7)
' Oops, there is something missing here. The value of i
If Ctrl.Object.Value <> arrParts(i, 7) Then
Ctrl.BackColor = &HC0C0FF
If MsgBox(WARNMSG, vbYesNo + vbDefaultButton2) = vbYes Then
' vbYes is choosen
Ctrl.BackColor = &H80000005
' Do something here
' ...
Else
' vbNo is choosen
Cancel = True
End If
Else
' Textbox value is equal to arrParts(i, 7)
Ctrl.BackColor = &H80000005
End If
End Sub
Private Sub CommandButton1_Click()
Me.Hide
Erase arrParts()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim i As Integer, ii As Integer, iii As Integer, iv As Integer
iii = 0
iv = 0
' ===== user has clicked the "Save changes" button to update the parts inventory records. First do
' validation tests to ensure good entries. If OK, write updated info to Parts worksheet =====
For i = 1 To UBound(arrParts)
If Me.Controls("TextBox" & 1 + iv).Tag = 2 And Me.Controls("TextBox" & 1 + iv).Value = "" And _
(Me.Controls("TextBox" & 1 + iv + 1).Value <> "" Or Me.Controls("TextBox" & 1 + iv + 2).Value <> "" Or _
Me.Controls("TextBox" & 1 + iv + 3).Value <> "" Or Me.Controls("TextBox" & 1 + iv + 4).Value <> "" Or _
Me.Controls("TextBox" & 1 + iv + 5).Value <> "" Or Me.Controls("TextBox" & 1 + iv + 6).Value <> "") Then
MsgBox "There is missing data that must be provided", vbExclamation + vbOK, "Missing Data"
Me.Controls("TextBox" & 1 + iv).SetFocus
Me.Controls("TextBox" & 1 + iv).SelStart = 0
Me.Controls("TextBox" & 1 + iv).SelLength = Len(Me.Controls("TextBox" & 1 + iv))
Exit Sub
ElseIf Me.Controls("TextBox" & 1 + iv).Tag = 2 And Me.Controls("TextBox" & 1 + iv).Value = "" Then
Me.Controls("ComboBox" & i).Value = ""
End If
Range("B" & i + 6) = Me.Controls("TextBox" & 1 + iv).Value
For ii = 2 To 7
iii = 2 * (ii - 1)
Range("B" & i + 6).Offset(0, iii) = Me.Controls("TextBox" & ii + iv).Value
Next ii
Range("B" & i + 6).Offset(0, iii + 2) = Me.Controls("ComboBox" & i).Value
iv = iv + 7
Next i
MsgBox "Click OK to finish updating your replacement parts inventory", vbInformation + vbOKOnly, "One Step Remains"
End Sub