Hi,
I have the written the following code but it is slow to run which is causing the end users issues, is there anyway of speeding it up?
Any help would be much appreciated.
Regards,
Damian
I have the written the following code but it is slow to run which is causing the end users issues, is there anyway of speeding it up?
Any help would be much appreciated.
Code:
Sub CheckButtons()
AppStart
Lr = TransferSht.Range("B" & Rows.Count).End(xlUp).Row
y = 96 + Lr
For x = 101 To y 'Number of transfers on LoadPlan userform
If AccessGroup < 2 Then
Set Rng = TransferSht.Range(TransferSht.Range("B5"), TransferSht.Range("B" & Rows.Count).End(xlUp))
For Each C In Rng
If C.Offset(, 1).Text = "Y" And C = x Then
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnPre" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnInspect" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnDesp" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnTimes" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnMissed" & x Then
Ctrl.Visible = True
End If
End If
If TypeName(Ctrl) = "Label" Then
If Ctrl.Name = "lbl" & x Then
Ctrl.Visible = True
End If
End If
Next Ctrl
End If
Next C
Set C = Nothing
End If
If AccessGroup > 1 Then
Set Rng = TransferSht.Range(TransferSht.Range("B5"), TransferSht.Range("B" & Rows.Count).End(xlUp))
For Each C In Rng
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnPre" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnInspect" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnDesp" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnTimes" & x Then
Ctrl.Visible = True
End If
If Ctrl.Name = "btnMissed" & x Then
Ctrl.Visible = True
End If
End If
If TypeName(Ctrl) = "Label" Then
If Ctrl.Name = "lbl" & x Then
Ctrl.Visible = True
End If
End If
Next Ctrl
Next C
Set C = Nothing
End If
Set Rng = DataSht.Range(DataSht.Range("C5"), DataSht.Range("C" & Rows.Count).End(xlUp))
For Each C In Rng
If C.Text = LoadPlan.txtCal.Text And C.Offset(, 1) = x And C.Offset(, 62) = "Y" Then 'Changes missed trailer button to yellow if no reason known
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnMissed" & x Then
Ctrl.BackColor = vbYellow
Ctrl.Caption = "Load " & C.Offset(, 35) & " Has Been Missed "
Ctrl.Width = 140
Ctrl.Left = 90
MissedLoadNum = "btnReason" & x
End If
If Ctrl.Name = MissedLoadNum Then
Ctrl.Visible = True
Ctrl.BackColor = vbYellow
Ctrl.Caption = "Reason Unknown"
Ctrl.Left = 250
Ctrl.Width = 100
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And C.Offset(, 1) = x And C.Offset(, 62) = "Y" And C.Offset(, 36) <> Empty Then 'Changes missed trailer button to red if reason known
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnMissed" & x Then
Ctrl.BackColor = vbRed
Ctrl.ForeColor = vbYellow
Ctrl.Left = 90
End If
If Ctrl.Name = MissedLoadNum And C.Offset(, 36) <> "" Then
Ctrl.Visible = True
Ctrl.BackColor = vbRed
Ctrl.ForeColor = vbYellow
Ctrl.Caption = C.Offset(, 36)
Ctrl.Left = 250
Ctrl.Width = 200
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 50) = "Y" Then 'Changes pre-checks buttons green if checks completed
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnPre" & x Then
Ctrl.BackColor = vbGreen
Ctrl.Caption = "Trailer No " & C.Offset(, 3)
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 53) = "Y" Then 'Changes inspection buttons green if checks completed
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnInspect" & x Then
Ctrl.BackColor = vbGreen
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 56) = "Y" Then 'Changes despatch buttons green if checks completed
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnDesp" & x Then
Ctrl.BackColor = vbGreen
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 59) = "Y" Then 'Changes despatch times buttons green if checks completed
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnTimes" & x Then
Ctrl.BackColor = vbGreen
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 50) = "Y" And C.Offset(, 62) = "Y" Then 'Checks for missed trailer
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnPre" & x Then
Ctrl.Visible = False
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 53) = "Y" And C.Offset(, 62) = "Y" Then 'Checks for missed trailer
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnInspect" & x Then
Ctrl.Visible = False
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 56) = "Y" And C.Offset(, 62) = "Y" Then 'Checks for missed trailer
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnDesp" & x Then
Ctrl.Visible = False
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 59) = "Y" And C.Offset(, 62) = "Y" Then 'Checks for missed trailer
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnTimes" & x Then
Ctrl.Visible = False
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 50) = "Y" Then 'Hides Missed Transfer button if Pre-Checks have been completed
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnMissed" & x Then
Ctrl.Visible = False
End If
End If
Next Ctrl
End If
If C.Text = LoadPlan.txtCal.Text And CLng(C.Offset(, 1)) = x And C.Offset(, 62) = "Y" Then 'Hides Missed Transfer button if Pre-Checks have been completed
For Each Ctrl In LoadPlan.Controls
If TypeName(Ctrl) = "CommandButton" Then
If Ctrl.Name = "btnPre" & x Then
Ctrl.Visible = False
End If
If Ctrl.Name = "btnInspect" & x Then
Ctrl.Visible = False
End If
If Ctrl.Name = "btnDesp" & x Then
Ctrl.Visible = False
End If
If Ctrl.Name = "btnTimes" & x Then
Ctrl.Visible = False
End If
End If
Next Ctrl
End If
Next C
Next x
AppEnd
End Sub
Regards,
Damian