Vba Excel 2016: type mismatch error

I am trying to copy a whole row on a sheet with the name “All Groups” (from A to AF) if the AG column contained a specific value and pasted it into a sheet with the name “Green” (if AG = 1, blue, if AG = 2 and Red if AG = 3).

However, I get a type mismatch error .

I looked through the forum and Internet posts with similar errors, but I could not 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
+4
source share
2 answers

CStr , . . .

, LSearchRow var, .

Sub all_group_sort()
    Dim LSearchRow As Long
    Dim LCopyToRow As Long

    With Worksheets("All groups")
        'Start search in row 3
        LSearchRow = 3

        'Start copying data to row 3 in Destination Sheet (row counter variable)
        LCopyToRow = 3  '= LSearchRow
        While CBool(Len(Range("A" & LSearchRow).Value2))
            'If value in column AG = "1", copy and paste entire row to Green. Then go back and continue searching
            Select Case CStr(.Range("AG" & LSearchRow).Value2)
                Case "1"
                    .Cells(LSearchRow, "A").Resize(1, 32).Copy _
                        Destination:=Worksheets("Green").Cells(LCopyToRow, "A")
                    LCopyToRow = LCopyToRow + 1
                Case "2"
                    .Cells(LSearchRow, "A").Resize(1, 32).Copy _
                        Destination:=Worksheets("Blue").Cells(LCopyToRow, "A")
                    LCopyToRow = LCopyToRow + 1
                Case "3"
                    .Cells(LSearchRow, "A").Resize(1, 32).Copy _
                        Destination:=Worksheets("Red").Cells(LCopyToRow, "A")
                    LCopyToRow = LCopyToRow + 1
                Case Else
                    'do nothing
                    Debug.Print "Not 1,2 or 3 - " & .Range("AG" & LSearchRow).Value2
            End Select
            LSearchRow = LSearchRow + 1
        Wend
    End With
End Sub

Select Case statement. ; .

0

CStr , VBA String, ( ) String.

, .Select - , , !

Worksheet ( Set, ), If Select Case , If.

, Paste !

, , , :

Sub sort()
Dim wS As Worksheet, _
    wsDest As Worksheet, _
    LSearchRow As Integer, _
    LCopyToRow As Integer

Set wS = Sheets("All groups")
LSearchRow = 3      'Start search in row 3
LCopyToRow = 3      'Start copying data to row 3 in Destination Sheet (row counter variable)

With wS
    While Len(CStr(.Range("A" & LSearchRow).Value)) > 0
        'Copy the row
        .Rows(LSearchRow & "A:AF" & LSearchRow).Copy
        'Select the sheet to paste on
        Select Case .Range("AG" & LSearchRow).Value
            Case Is = 1
                Set wsDest = Sheets("Green")
            Case Is = 2
                Set wsDest = Sheets("Blue")
            Case Is = 3
                Set wsDest = Sheets("Red")
            Case Else
                'Cover unknown cases
                MsgBox "Case not cover by code, press Ctrl+Break to stop code and debug", vbCritical + vbOKOnly
        End Select
        'Calculate first empty row on destination sheet
        LCopyToRow = wsDest.Range("A" & wsDest.Rows.Count).End(xlUp).Row + 1
        'Paste the copied row
        wsDest.Rows(LCopyToRow & "A:AF" & LCopyToRow).Paste
        'Continue to next line
        LSearchRow = LSearchRow + 1
    Wend
End With

End Sub
0

Source: https://habr.com/ru/post/1618998/


All Articles