I am trying to copy a entire row on a sheet named "All groups" (A to AF) if column AG contained a certain value and paste it into a sheet named "Green" (if AG =1, blue if AG=2 and Red if AG=3 ).
However, i get a type mismatch error.
I have looked on the forum and the internet of posts of similar errors but i wasn't able to find an answer that would help me. I am using Excel 2016 and Here is my code:
Sub sort()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Worksheets("All groups").Select
'Start search in row 3
LSearchRow = 3
'Start copying data to row 3 in Destination Sheet (row counter variable)
LCopyToRow = 3
While Len(Range("A" & CStr(LSearchRow)).Value) > 0
'If value in column AG = "1", copy and paste entire row to Green. Then go back and continue searching
If Range("AG" & CStr(LSearchRow)).Value = "1" Then
Rows(CStr(LSearchRow) & "A:AF" & CStr(LSearchRow)).Select
Selection.Copy
Worksheets("Green").Select
Rows(CStr(LCopyToRow) & "A:AF" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
LCopyToRow = LCopyToRow + 1
Worksheets("All groups").Select
'If value in column AG = "2", copy and paste entire row to Blue. Then go back and continue searching
ElseIf Range("AG" & CStr(LSearchRow)).Value = "2" Then
Rows(CStr(LSearchRow) & "A:AF" & CStr(LSearchRow)).Select
Selection.Copy
Worksheets("Blue").Select
Rows(CStr(LCopyToRow) & "A:AF" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
LCopyToRow = LCopyToRow + 1
Worksheets("All groups").Select
'If value in column AG = "3", copy and paste entire row to Red. Then go back and continue searching
Else
Rows(CStr(LSearchRow) & "A:AF" & CStr(LSearchRow)).Select
Selection.Copy
Worksheets("Red").Select
Rows(CStr(LCopyToRow) & "A:AF" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
LCopyToRow = LCopyToRow + 1
Worksheets("All groups").Select
End If
Wend
End Sub
Rows(CStr(LSearchRow) & "A:AF" & CStr(LSearchRow))shpuld beRows(CStr(LSearchRow) & ":" & CStr(LSearchRow))- R3uKRows(...statement... Anyway, as you don't get an highlighted row, I suggest that you place breakpoints on almost every line to narrow down the culprit line. Are you used to debug like this? If not I'll propose a curated solution for what you are trying to do! ;) - R3uK