I have a partially working VBA macro which copies from one sheet 'AverageEarnings' to one of two ('SicknessRecordGraded', 'SicknessRecordUngraded') on condition that a string is found in column 41 ("GRADED", "UNGRADED").
I want the macro to copy values from columns B,C ('AverageEarnings') to columns A, B on the other sheets, from row 3.
At the moment it is copying columns B,C,D to columns A,B,C on the other sheet from row 3.
Furthermore, I am receiving far too many values from the initial sheet, of which there are 4957. Here is my macro.
Public Sub CopyRowsSickness()
' This macro will copy rows in AverageEarnings spreadsheet, to Sheet1 if Ungraded (Column AO) or to Sheet2 if Graded.
' Insert message box to warn user.
If MsgBox("This could take some time. (5 - 10 mins). Proceed?", vbYesNo) = vbNo Then Exit Sub
' Select initial sheet to copy from
Sheets("AverageEarnings").Select
' Find the last row of data - xlUp will check from the bottom of the spreadsheet up.
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' For loop through each row
For x = 1 To FinalRow
ThisValue = Cells(x, 41).Value ' The value to check is located in Column AO or 41.
If ThisValue = "UNGRADED" Then ' If the value is Ungraded
Cells(x, 2).Resize(5000, 3).Copy ' Resize from intial range.
Sheets("SicknessRecordUngraded").Select ' Specify first sheet to copy to.
NextRow = Cells(3, 3).End(xlUp).Row + 1 ' <-- This line instead of next if you dont want paste over top.
'NextRow = Cells.Row + 1
' Rows.Count, 1
Cells(NextRow, 1).Select ' Find the next row.
ActiveSheet.Paste ' Paste information.
Sheets("AverageEarnings").Select 'Reselect sheet to copy from.
ElseIf ThisValue = "GRADED" Then
Cells(x, 2).Resize(5000, 3).Copy
Sheets("SicknessRecordGraded").Select
NextRow = Cells(3, 3).End(xlUp).Row + 1
'NextRow = Cells.Row + 1 ' Increment row.
' Rows.Count, 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("AverageEarnings").Select
End If
Next x
End Sub
I am aware the problem lies within the values of these two lines. I have amended them to a variety of different values, but have not managed to fix it yet. It seems to paste far more values than the existing in 'AverageEarnings'.
Cells(x, 2).Resize(5000, 3).Copy
NextRow = Cells(3, 3).End(xlUp).Row + 1