Sub Checkdata()
Dim i, j As Integer
Dim sSheet, sCell As String
Rem 1 - find bottom line of sheet1
i = FindLastRow("Sheet1")
j = 0
' MsgBox (CStr(i))
Rem 2 - check that row not already copied and that required fields are entered
If Range("F" & i).Value = "Transferred" Then
MsgBox ("Data already copied - cannot continue")
Exit Sub
End If
If (IsEmpty(Range("A" & i))) Or (IsEmpty(Range("B" & i))) Or (IsEmpty(Range("E" & i))) Or (IsEmpty(Range("C" & i)) And IsEmpty(Range("D" & i))) Then
MsgBox ("Not all data entered - please correct")
Exit Sub
End If
Rem 3 - check that data is correct
' a valid date in column A
If Not (IsDate(Range("A" & i))) Then
MsgBox ("Not a valid date in column A - please correct")
Exit Sub
End If
' either C or D, but not both, contain a positive value
If (Val(Range("C" & i)) > 0) And (Val(Range("D" & i)) > 0) Then
MsgBox ("Cannot have a value in both receipts and expenditure columns - please correct")
Exit Sub
End If
' cannot have a negative
If (Val(Range("C" & i)) < 0) Or (Val(Range("D" & i)) < 0) Then
MsgBox ("Cannot have a negative value - please correct")
Exit Sub
End If
Rem 4 - if type is input then get bottom row of sheet2 and then copy relevent cells
Rem - if type is output then as above but for sheet 3
If Val(Range("C" & i)) > 0 Then
sSheet = "Sheet2"
sCell = "C"
ElseIf Val(Range("D" & i)) > 0 Then
sSheet = "Sheet3"
sCell = "D"
End If
' get bottom filled row of sheet to copy to and add 1 to it to get next clear row
j = FindLastRow(sSheet) + 1
'MsgBox (sSheet & " Row:" & CStr(j))
'Need to copy cells A, E and either C or D to relevent sheet cells A, B and C
Sheets(sSheet).Range("A" & j).Value = Range("A" & i).Value
Sheets(sSheet).Range("B" & j).Value = Range("E" & i).Value
Sheets(sSheet).Range("C" & j).Value = Range(sCell & i).Value
Rem 5 - now add copied comment to Sheet1
Range("F" & i).Font.Color = vbBlue
Range("F" & i).Value = "Transferred"
End Sub
'==========================================
'Find the last used Row on a Worksheet
'==========================================
Function FindLastRow(sSheet) As Integer
Dim afRange As String, afRangeNrows As Long, afRangeNcols As Long
Dim afRangeLastRow As Variant, afRangeLastCol As Variant
Dim lastRow As Variant
lastRow = Worksheets(sSheet).Cells(Rows.Count, 1).End(xlUp).Row
' Abandoned line below as always returned last formatted row even if empty
'lastRow = Sheets(sSheet).Cells.SpecialCells(xlCellTypeLastCell).Row
FindLastRow = lastRow
End Function