vendredi 8 novembre 2019

VBA Script using For Loop and If/Then statement

I'm still new to VBA and currently writing a code to try and check whether a column has a specific value in each row. It requires the use of a for loop and an if/then statement.

I wrote the script but when I execute it, it does nothing. Can someone help me check if my code is written correctly?

Sub TestCheck()

    Dim Rng As Range
    Dim xlsheet As Object
    Dim c As Range
    Dim LastRow As Long

    'Checks each cell value in column A for string - "Iris Concept of Operations"'
    n = 0

    Set xlsheet = ActiveSheet
    With xlsheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    If .Cells(.Rows.Count, "A").End(xlUp).Row = 0 Then
        MsgBox "The spreadsheet doesn't work"
        GoTo ExitHere

    Set Rng = .Range(LastRow).Offset(n, 0)

    For Each c In Rng
        n = n + 1

        If InStr(c.Value, "Iris Concept of Operations") > 0 Then
            With xlsheet
                .Range("A1").Offset(n, 8) = c.ClearContents
            End With
        Else
            With xlsheet
                .Range("A1").Offset(n, 2) = c.ClearContents
            End With
        End If
    Next c

    End If

    ExitHere:
        Set xlsheet = Nothing
        Set Rng = Nothing
    End With

End Sub

Aucun commentaire:

Enregistrer un commentaire