Thanks for the input Bob and Tom,
I've had to issue my spreadheets to the users now to meet a deadline but am
planning on writing some audit codes to validate the data once the
spreadsheets are returned to me.
I've found this code as a start.... any better/simpler ideas are welcome!
I will need to check formulas haven't had values pasted over them.
Also make sure comments column has been filled in if margin diff is over 10%.
Sub AuditSheet()
' Carry out an Audit of all active rows in a spreadsheet.
Dim nRows As Long
Dim RN As Long
Dim nCols As Long
Dim EMsg As String
'On Error GoTo errHandler
With ActiveWorkbook.ActiveSheet
With .Cells(.Rows.Count, 1)
nRows = .End(xlUp).Row
End With
End With
For RN = 2 To nRows 'check each row in turn (assume row 1 is
header)
EMsg = ChkCol1(RN)
If EMsg <> "" Then
Call ProcessError(EMsg, RN, 1)
End If
EMsg = ChkCol2(RN)
If EMsg <> "" Then
Call ProcessError(EMsg, RN, 2)
End If
EMsg = ChkCol3(RN)
If EMsg <> "" Then
Call ProcessError(EMsg, RN, 3)
End If
Next RN
errHandler:
If Err.Number <> 0 Then
MsgBox (vbCrLf & "An Error Ocurred. Error Number: " & Err.Number & "
" & Err.Description & vbCrLf)
Else
MsgBox ("Checking Complete")
End If
End Sub
Private Sub ProcessError(ErrMsg As String, rrr As Long, ccc As Long)
Dim ret As Integer
Dim ErrTitle As String
Cells(rrr, ccc).Activate
ErrTitle = " Error Found In Cell - " & ActiveCell.Address & " "
ErrMsg = ErrMsg & vbCrLf & vbCrLf & "Continue Checking?"
ret = MsgBox(ErrMsg, vbYesNo, ErrTitle)
If ret <> vbYes Then
End
End If
End Sub
Private Function ChkCol1(RowNum As Long) As String
Dim ErrMsg As String
ErrMsg = ""
If Cells(RowNum, 1).HasFormula = True Then
ErrMsg = ErrMsg & "Cell contains a formula." & vbCrLf
End If
If IsNumeric(Cells(RowNum, 1).Value) <> True Then
ErrMsg = ErrMsg & "Cell must contain a number." & vbCrLf
ElseIf Cells(RowNum, 1).Value > 10 Or Cells(RowNum, 1).Value < -10 Then
ErrMsg = ErrMsg & "Cell value should be between -10 and 10." & vbCrLf
End If
' Other tests include Isempty, IsLogical, IsDate, IsText
ChkCol1 = ErrMsg
End Function
Private Function ChkCol2(RowNum As Long) As String
Dim ErrMsg As String
ErrMsg = ""
If Cells(RowNum, 2).HasFormula = True Then
ErrMsg = ErrMsg & "Cell contains a formula." & vbCrLf
End If
If IsDate(Cells(RowNum, 2).Value) <> True Then
ErrMsg = ErrMsg & "Cell must contain a date." & vbCrLf
End If
ChkCol2 = ErrMsg
End Function
Private Function ChkCol3(RowNum As Long) As String
Dim ErrMsg As String
ErrMsg = ""
If Cells(RowNum, 1).Value < 5 Then
If IsEmpty(Cells(RowNum, 3).Value) = True Then
ErrMsg = ErrMsg & "If Column A is less than 5, Column C must
explain why." & vbCrLf
End If
End If
ChkCol3 = ErrMsg
End Function