Private Sub Workbook_Open()
Sheets("Regression Outlier Table").Activate
Range("A1:I1").Select
With Selection.Font
.Name = "Arial Narrow"
.Size = 13
End With
Sheets("Diagnostic Measures").Activate
Range("B3").Select
End Sub
-----------------------------------------------------------------------------------
Private Sub ClearData_Click()
ResetSheets.ClearData
End Sub
-----------------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngempty As Long
Dim txtinrange As Long
Dim pnvalue As Integer
Dim pval As Integer
Dim nval As Integer
txtinrange = WorksheetFunction.CountIf(Range("PastedData"), "*")
rngempty = WorksheetFunction.CountA(Range("PastedData"))
pnvalue = WorksheetFunction.CountA(Range("pnData"))
'If the range is empty or contains text then exit the subroutine.
If rngempty = 0 Then Exit Sub
If txtinrange > 0 Then Exit Sub
'Format the four header rows should the user paste the headings from Minitab.
Range("B13").Select
ActiveCell.FormulaR1C1 = "SRES"
Range("C13").Select
ActiveCell.FormulaR1C1 = "HI"
Range("D13").Select
ActiveCell.FormulaR1C1 = "COOK"
Range("E13").Select
ActiveCell.FormulaR1C1 = "DFITS"
Range("B13:E13").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Arial"
.Size = 12
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeColor = xlThemeColorDark1
End With
With Selection.Interior
.Pattern = xlSolid
.PatternThemeColor = xlThemeColorAccent1
.ThemeColor = xlThemeColorAccent1
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'Format the "PastedData" table with borders, font size and font type
'if the user happens to not paste the values.
Range("PastedData").Select
With Selection.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("PastedData").Interior.Color = RGB(220, 230, 241)
With Selection.Font
.Name = "Arial"
.Size = 12
End With
'Conditional formatting applied to SRES range.
Range("SRESrng").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
Formula1:="=-2", Formula2:="=2"
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
End With
With Selection.FormatConditions(1).Borders(xlLeft)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
End With
Selection.FormatConditions(1).StopIfTrue = False
'Conditional formatting applied to HI range.
Range("HIrng").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="='Regression Outlier table'!$E$3"
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
End With
With Selection.FormatConditions(1).Borders(xlLeft)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
End With
Selection.FormatConditions(1).StopIfTrue = False
'Conditional formatting applied to Cook range.
Range("Cookrng").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
, Formula1:="=1"
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
End With
With Selection.FormatConditions(1).Borders(xlLeft)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
End With
Selection.FormatConditions(1).StopIfTrue = False
'Conditional formatting applied to DFITS range.
Range("DFITSrng").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
Formula1:="=-1", Formula2:="=1"
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
End With
With Selection.FormatConditions(1).Borders(xlLeft)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlRight)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlTop)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Borders(xlBottom)
.LineStyle = xlContinuous
.Color = -16776961
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.599993896298105
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("B3").Select
'Turn Screen Updating Off
Application.ScreenUpdating = False
'If either the p or n value is missing, open UserForms dialog.
If pnvalue = 2 Then GoTo Nextline
If pnvalue <= 1 Then
pnval.Show
End If
Nextline:
'Copy/Paste data from Dianostic Measures worksheet
'to the Summary Unsorted worksheet and filter Outliers by removing blank cells.
Range("RawDataTable").Copy
Sheets("Summary Unsorted").Activate
ActiveSheet.Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("$A$1:$G$10001").AutoFilter Field:=6, Criteria1:="<>"
ActiveSheet.Range("A1").Select
'Copy/Paste data from the Dianostic Measures worksheet
'to the Summary Sorted worksheet and sort by Cnt-descending, Obs-ascending
'and filter the Outliers by rmoving the blank cells.
Range("RawDataTable").Copy
Sheets("Summary Sorted").Activate
ActiveSheet.Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Sheets("Summary Sorted").Sort.SortFields.Add Key:=Range( _
"G2:G10001"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
Sheets("Summary Sorted").Sort.SortFields.Add Key:=Range( _
"A2:A10001"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Summary Sorted").Sort
.SetRange Range("A1:G10001")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'The immediate line below prevents the "Excel found unreadable content..."
'error when opening.
ActiveWorkbook.Worksheets("Summary Sorted").Sort.SortFields.Clear
ActiveSheet.Range("$A$1:$G$10001").AutoFilter Field:=6, Criteria1:="<>"
ActiveSheet.Range("A1").Select
'Activate the Summary Unsorted worksheet
Sheets("Diagnostic Measures").Activate
ActiveSheet.Range("B3").Select
'Turn Screen Updating On
Application.ScreenUpdating = True
'Display Message Box that the Summaries have finished.
MsgBox "Summaries Completed Successfully", vbOKOnly, "Complete"
'Protect the "Diagnostic Measures worksheet from changes.
ActiveSheet.Protect "password", True, True
End Sub
----------------------------------------------------------------------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
----------------------------------------------------------------------------------
Sub ClearData()
'ClearData Macro
'This Macro deletes the cell contents from the two summary worksheets
'and deletes the cell contents from the pasted data table.
'Turn Screen Updating Off
Application.ScreenUpdating = False
'Activate the Summary Unsorted worksheet, clear the filter and delete the data.
Sheets("Summary Unsorted").Activate
On Error Resume Next
ActiveSheet.Range("$A$1:$G$10001").AutoFilter Field:=6
Selection.CurrentRegion.Select
Selection.ClearContents
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
Range("A1").Select
'Activate the Summary Sorted worksheet, clear the filter and delete the data.
Sheets("Summary Sorted").Activate
ActiveSheet.Range("$A$1:$G$10001").AutoFilter Field:=6
Selection.CurrentRegion.Select
Selection.ClearContents
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
Range("A1").Select
'Activate the Diagnostic Measures worksheet, delete the data in the range
'and delete p and n values.
Sheets("Diagnostic Measures").Activate
ActiveSheet.Unprotect "password"
Range("PastedData").Select
Selection.ClearContents
Range("B3:B4").Select
Selection.ClearContents
Range("B3").Select
End Sub
----------------------------------------------------------------------------------
Private Sub OKButton_Click()
'The App.EnableEvents code below prevents the
'Run-time error '400': Form already displayed; can't show modally
Application.EnableEvents = False
Range("B3").Value = Val(txtpval.Text)
Range("B4").Value = Val(txtnval.Text)
'Checks for an empty field for the p value.
If Trim(Me.txtpval.Value) = "" Then
Me.txtpval.SetFocus
MsgBox "Please enter a whole number for both p and n", , "Empty Field"
Exit Sub
End If
'Checks to see if p value was exceeded.
If Trim(Me.txtpval.Value) > 1000 Then
Me.txtpval.SetFocus
MsgBox "Enter p value <= 1,000", , "Number Exceeded"
Exit Sub
End If
'Checks for an empty field for the n value.
If Trim(Me.txtnval.Value) = "" Then
Me.txtnval.SetFocus
MsgBox "Please enter a whole number for both p and n", , "Empty Field"
Exit Sub
End If
'Checks to see if n value was exceeded.
If Trim(Me.txtnval.Value) > 10000 Then
Me.txtpval.SetFocus
MsgBox "Enter n value <= 10,000", , "Number Exceeded"
Exit Sub
End If
'The App.EnableEvents code below prevents the
'Run-time error '400' (see first comment)
Application.EnableEvents = True
Unload Me
End Sub
-----------------------------------------------------------------------------------
Private Sub txtpval_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii > 47 And KeyAscii < 58) Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
MsgBox "Only whole numbers are accepted for both p and n.", , "Whole Numbers Only"
End If
End Sub
-----------------------------------------------------------------------------------
Private Sub txtnval_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii > 47 And KeyAscii < 58) Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
MsgBox "Only whole numbers are accepted for both p and n.", , "Whole Numbers Only"
End If
End Sub
-----------------------------------------------------------------------------------
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please click the OK Button after entering whole numbers!", , "Values Needed"
End If
End Sub