locked
Loop checking string, then replacing values RRS feed

  • Question

  • Hi all,

    I feel like this should be extremely straightforward, and yet I keep getting a syntax error. I'm just trying to write a code that checks column 5 for an "x", and if it is there, replaces the value in column 4 (a date) with the value from column 6 (a string). What am I missing here?

    Thank you!

    Dim i As Integer

    For i = 2 To 200

    If Cell(i, 5).Value = "x"

        Then Cells(i, 4).Value = Cells(i, 6).Value

        Else

    End If

     

    Next i

    End Sub


    Friday, September 22, 2017 3:44 PM

All replies

  • Sub Fixed()

    Dim i As Integer

    For i = 2 To 200

    If Cell(i, 5).Value = "x" Then

    Cells(i, 4).Value = Cells(i, 6).Value

    End If

    Next i

    End Sub

    Friday, September 22, 2017 5:14 PM
  • That worked, thank you! Is it literally just that I needed the 'Then' to be on the same line as the 'If'? Guess I'm getting my own wires crossed as I switch between syntaxes. Thanks again!
    Friday, September 22, 2017 8:18 PM
  • The simpler syntax with no action to be taken if the condition is false is

    If Cells(i,5).Value = x Then Cells(i,4).value = Cells(i,6).Value

    Next i

    Monday, October 30, 2017 8:30 AM